views:

212

answers:

2

I know from here that I can pass an ignore option in the jQuery.validate method like this...

$('form').validate({
    ignore: ":hidden"
});

... which works fine, but I wanted to set the validator to ignore ":hidden" form fields by default... I've tried the following:

jQuery.validator.defaults.ignore.push(":hidden");

... but it doesn't work.

To hide the form fields I've wrapped them in a div and I hide the div; which by default has "display: none" applied.

Anyone have any ideas?

Edit: I've been wondering after a bit of a look in firebug, whether the "not()" method in jQuery is not meant to be able to take an array... which is what the jQuery.validator.defaults.ignore property is, or is it something else?

Edit 2 I've noticed that if I do this:

jQuery.validator.defaults.ignore = ":hidden";

it works... again, i think it might be that it is an array...

Results It seems that using the code in Edit 2 above is not unreasonable as when you assign 'ignore: ":hidden"' in the validate() method you are essentially doing the same things... What got me was that the item in the defaults is an array.

A: 

Though the defaults.ignore property is an array it is passed to the jQuery not() method, which takes a selector... so by assigning a string as follows, the validator ignores elements out of view. This works:

jQuery.validator.defaults.ignore = ":hidden"; 

The only real question i would have is why it is an array in the first place. It's defined like this:

ignore: []
jwwishart
+1  A: 

One of the overloads for .not() is .not([elements]), see here for details. The initial declaration of [] is passing an empty set of elements, leaving nothing for jQuery to remove. When you change the type to a string, you're calling a different overload of .not()

Nick Craver
Thanks, I didn't realize this. It explains why it wasn't working... which is because i wasn't passing elements (I was trying to pass an array of jQuery selectors. I've already given you a +1, you can have the answer too as you've explained why it wasn't working the first way...
jwwishart