Reputation: 2325
I have a form which I want to use with jQuery unobtrusive validation. Everything works fine.
But I also have other forms on that view, like a search form, login form, etc., and the plugin attaches to all those forms as well, and I don't want this.
How can I explicitly state which forms the plugin should attach to, and which not?
Upvotes: 0
Views: 603
Reputation: 33
You can destroy the instance of validator for a particular form with
$("#[form_Id]").data("validator").destroy();
See https://jqueryvalidation.org/Validator.destroy/
Upvotes: 0
Reputation: 6086
I think you have two choices, as this option does not come out of the box.
I would go for 1. and I'm not 100% sure about how you would do 2, although I recall seeing something about it on SO.
So, if you look into the (unminified) jquery.validate.unobtrusive.js file around line 203 you see this:
var $forms = $(selector)
.parents("form")
.andSelf()
.add($(selector).find("form"))
.filter("form");
I would change that to
var $forms = $(selector)
.parents("form.include")
.andSelf()
.add($(selector).find("form.include"))
.filter("form.include");
so this will only validate forms with class 'include'
Upvotes: 2