0

I have model with couple of regular expression on properties as below:

The view is dynamic view, that I am loading its content from jquery as below:

UPDATE: I found the issue but not the solution. The regular expression client side attributes are not rendered because I am using [UIHint("TextBox")] to add some custom classes and scripts. If I remove UIHInt, all validation attributes are generated without any issue. If I add UIHInt nothing generates.

Not sure what is wrong in my UIHInt editor template. pls advise???

EditorTemplate/TextBox.cshtml

@Html.TextBoxFor(m => Model, new {@class="txt"})

my model:

public partial class PartyRole
{
    [UIHint("TextBox")]
    [RegularExpression(@"^.{5,}$", ErrorMessage="Minimum 5 characters required")]
    [StringLength(50, ErrorMessage="Maximum {2} characters exceeded")]
    public string Title { get; set; }
}

jquery unobtrusive is included in my bundles as:

bundles.Add(new ScriptBundle("~/js/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

View:

@model dynamic

@using (Html.BeginForm("Edit", null, FormMethod.Post, new { id="FrmIndex" }))
{
@Html.ValidationSummary(true);
@Html.EditorForModel()

 <input type="submit" value="Edit" />
}
<script>
$(".datetime").datetime();
$(".combo").combo();
$(".chk").chk();

var form = $("#FrmIndex")
          .removeData("validator") /* added by the raw jquery.validate plugin */
          .removeData("unobtrusiveValidation");  /* added by the jquery unobtrusive plugin */
$.validator.unobtrusive.parse(form);

</script>

But still the the validations are not generated on client side. The message are generating from server side.

UPDATE: got some break through. These validations are working fine in my strongly typed views. However not in my dynamic view that I am currently showing here.

can somebody advise what is wrong I am doing here?

mmssaann
  • 1,507
  • 6
  • 27
  • 55
  • Can you show the source code of the view? Perhaps you do not show the validation message in your view or have disabled validations in App_Start/web.config. – glautrou Jul 29 '13 at 06:55
  • updated question with view. i didnt disable anything in web.config. what do u mean by "you do not show the validation message in your view"? unobtrusive validation is working fine for [Required] data annotations.but for regular expression, it is showing messages from server/. – mmssaann Jul 29 '13 at 07:00
  • Thanks, why do you add the extra JavaScript for validation at the end of your view? I am not sure that will fix the problem but check [this link - section Enabling Client-Side Validation](http://www.asp.net/mvc/tutorials/older-versions/javascript/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript) – glautrou Jul 29 '13 at 10:09
  • my view is dynamic that is getting loaded into a jquery dialog. so I need to re-parse the scripts as I did in the dynamic view. This is working for [Require] dataannotations but not for [RegularExpressions]. – mmssaann Jul 29 '13 at 10:47

1 Answers1

1

In your custom editor template you should trick the helper into thinking that it is inside a FormContext so that it emits the HTML5 data-* attributes on the input field which are used by unobtrusive validation. So in your template put the following and you are good to go:

@{
    this.ViewContext.FormContext = new FormContext();
}

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class="txt" })
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928