jquery.validate v. 1.9 ignores some hidden inputs
Asked Answered
F

1

2

with version 1.7 everything works ok, all hidden inputs get validated,
but with version 1.9 some do and some don't
I use asp.net mvc 3 and jquery.validate + jquery.unobtrusive (jquery 1.7.1)

this is the generated html:

    <!--this gets validated-->
        <input type="hidden" data-val="true" data-val-number="The field Chef must be a number." data-val-required="The Chef field is required." value="" name="Chef" id="Chef">    
<span data-valmsg-replace="true" data-valmsg-for="Chef" class="field-validation-valid"></span>

    <!--this one is ignored-->
        <input type="hidden" data-val="true" data-val-number="The field MyFruit must be a number." data-val-required="The MyFruit field is required." value="" name="MyFruit" id="MyFruit">
<span data-valmsg-replace="true" data-valmsg-for="MyFruit" class="field-validation-valid"></span>

anybody knows why could this happen ?

Forfeit answered 19/12, 2011 at 17:37 Comment(0)
R
7

With 1.9 version validation plugin ignores :hidden elements by default.

Another change should make the setup of forms with hidden elements easier, these are now ignored by default (option “ignore” has “:hidden” now as default). In theory, this could break an existing setup. In the unlikely case that it actually does, you can fix it by setting the ignore-option to “[]” (square brackets without the quotes).

Because you're using unobtrusive version, you can't set any option. So you don't init plugin yourself, therefor you have to change it's setting after it's initialized. You can fix it like this:

var validatorSettings = $.data($('form')[0], 'validator').settings;
validatorSettings.ignore = "";

This code works for first form element in the markup, you can specify your form(s) and change default behaviour.

Runic answered 19/12, 2011 at 17:50 Comment(9)
Didn't you get this backwards in your code example? "square brackets without the quotes"Brahms
the first input gets validated even though it's hiddenForfeit
How exactly does that clear anything up? The documentation specifically says to use square brackets without quotes, []. Yet instead, you're using quotes without square brackets "".Brahms
@Sparky672 with "" it worked (for one more input, not all), with [] it didn't, @Emre Erkan, what should I do to set this settings to all forms not just to first one ?Forfeit
apparently some hidden disabled inputs also don't get validated (some do), this rule can't be clearedForfeit
@Chuck, I'm not claiming it will work or not, otherwise I'd post it as my own answer. However, the official documentation is pretty clear when it says to use "brackets without quotes"; so I'm simply wondering if Emre can explain why he's leaving out the brackets and replacing them with quotes.Brahms
@Sparky672 probably he knows that the way it says doesn't workForfeit
@ChuckNorris Validation plugin won't validate submit, reset, image and disabled inputs. You can check it's source; Line 454 and Line 455.Runic
@Sparky672 I used it that way before and it worked well. [] or "" I checked the code, it's using .not() method for excluding those elements, so it's no problem [] or "".Runic

© 2022 - 2024 — McMap. All rights reserved.