Fixing jQuery Validation with changing validators

Got jQuery Validation running, and now I know that it requires every input and select to have a name property; otherwise it will simply ignore your validation rules:

jquery.validate.js line 130:
staticRules[] = existingRules;

It will however still apply the CSS-based rules, so finding that out was a bit tricky.

Next stop: I create a dialog with a dropdown to select some sort of object type, each object type having different attributes and different validation rules.

Of course, the validation rules have to be re-defined every time the dropdown value changes. If there is already a validator defined on the form, you need to destroy the existing validator

var form = $('#myForm).get(0);
$.removeData(form, 'validator');

(found here)

Still, some mysterious bit of code still remains, as validation is still executed even though the validator had been deleted and the dropdown is ignored in the (non-existing?) validator:

Uncaught TypeError: Cannot read property 'settings' of undefined

says Chrome, and IE reports

Unable to get value of the property 'settings': object is null or undefined

The culprit is the method delegate() (line 313), which does not check whether a validator exists:

function delegate(event) {
  var validator = $.data(this[0].form, "validator"),
  eventType = "on" + event.type.replace(/^validate/, "");
  // this fixes handling the deleted validator:
  if (!validator) return;
  validator.settings[eventType] && validator.settings[eventType].call(validator, this[0], event);

5 Responses to Fixing jQuery Validation with changing validators

  1. freak3dot says:

    I struggled with the settings is null or not an object error for a while on one of my validators. I searched around the internet and didn’t find the answer to the issue I was having. I also found that all the top results in Google are closed. I’m posting the answer here as it somewhat relates to your post. If you have an HTML5 form attribute on your elements, this might be the cause of the settings is null or not an object error in IE. IE does not support the HTML5 form attribute.

    • Thomas says:

      how do you fix it? I’m using the html5 form as well. I did try this change “if (!validator) return;” and it works but may not be the best resolution of it since it causes the validate script to have side effects like it does not trigger the “onkeyup” or “onblur” events at all. I’m clueless at this point so any insight from you will be really appreciate. Thanks for the info ;- )

    • erjoell says:

      How can that be the case when the app is running in IE from the debug of VS 2012?

  2. Jlakshmi says:

    This is Great. Thanks for the fix

  3. sami says:

    Thank you.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: