DotNetShark
DotNetShark

Reputation: 159

Can i use mvc.net validation attributes inside kendo templates?

I'm using mvc html textbox helper inside kendo template. I get incorrect validation attribute value for regexp validation. For example "^([a-zA-Z0-9\s-]){0,}$" becomes "^([a-zA-Z0-9s-]){0,}$" - symbol "\" missing. When i'm trying to escape symbols on the server side it becomes valid on the client side and unvalid on the server but i need valid regexp on both. How i can fix this problem. I need to use mvc helper because it retrieves validation attributes from model metadata.

Upvotes: 1

Views: 737

Answers (1)

Vlad Omelyanchuk
Vlad Omelyanchuk

Reputation: 3091

Indeed the slashes within kendo template are used to escaped special symbols. In your case I would suggest you to escape the slahes with extra shales.

Here is what I tried on my side:

<form id="myForm" action="/" method="post">
    <div id="result">  
    </div>


<input type="submit" value="Go" />
</form>

<script id="foo" type="text/kendo">
    templatE:

    @Html.TextBoxFor(m=>m.Name)
</script>

<script type="text/javascript">
    var html = $('#foo').html().replace(/\\/g, '\\\\');   
    html = html.replace(/#/g, /\#/);
    var template = kendo.template(html);

    $('#result').html(template({}));
    $('#myForm').kendoValidator();
</script>

Where the model is decorated with the following attribute:

[RegularExpression(@"^([a-zA-Z0-9\s\-]){0,}$")]
public string Name { get; set; }

Upvotes: 1

Related Questions