Search code examples
jqueryasp.net-mvcjquery-validateunobtrusive-validation

JQuery: Validator.SetDefaults for a single form


I'm using JQuery validation and I have some custom logic inside showErrors for displaying errors.

     var form = $('#container').find('#form1');
     form.data('validator', null); //doing this so that newly added DOM elements are picked up.

            $.validator.setDefaults({

              showErrors: function (errorMap, errorList) {
                  ......
                  .....
              }
});

$.validator.unobtrusive.parse(form); 
form.validate();

This works absolutely fine. But the validator defaults are being set for all the forms on my page which I don't want. I'm interested just in setting the defaults for just form1

Also for some reason the below does not work for me.So I had to go with the above code and set the showErrors through $.validator.setDefaults.

form.validate({
    showErrors: function (errorMap, errorList) {
     .....
     ////
}
});

Any ideas on how to set the default showErrors for a single form on my page?


Solution

  • I'm answering my own question. Hope this helps someone running into a similar issue.

    Setting :

    form.data("validator").settings.showErrors = "..." did the trick for me.