Bobby B
Bobby B

Reputation: 2325

Prevent unobtrusive validation on specific form

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

Answers (2)

liamjmc
liamjmc

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

politus
politus

Reputation: 6086

I think you have two choices, as this option does not come out of the box.

  1. edit the source of the unobtrusive plugin
  2. unbind the validator on the forms that you don't want validation.

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

Related Questions