jquery-validation icon indicating copy to clipboard operation
jquery-validation copied to clipboard

setDefaults not working + suggested solution

Open bresleveloper opened this issue 10 years ago • 16 comments

hi the jQuery.validator.setDefaults({...}) is not working and not updating the settings. i've suggested a fix here http://stackoverflow.com/questions/11612939/invalidhandler-setdefault-ignored-in-validator-call-jquery-validation-plugin/24369083#24369083

bresleveloper avatar Jun 23 '14 15:06 bresleveloper

Thanks for the report, but this ticket nor the SO question provide a testcase demonstrating the issue. I need that in order to review and fix an issue.

jzaefferer avatar Jul 01 '14 18:07 jzaefferer

hi the testcase demonstrating the issue is just initializing a validator object with some settings and then calling the .setDefaults in order to try and change them, for example add a function to invalidHandler, example: $(...).validate(); //try to submit an invalid form jQuery.validator.setDefaults({ invalidHandler: function(event, validator) { // 'this' refers to the form var errors = validator.numberOfInvalids(); alert(errors) ; } }); //try to submit an invalid form, you should get an alert with a number, yet you dont. //documentation: http://jqueryvalidation.org/jQuery.validator.setDefaults/

hope that helps, if not please address me again

bresleveloper avatar Jul 01 '14 23:07 bresleveloper

This is actually working fine with the latest release.

nosachamos avatar Oct 18 '14 06:10 nosachamos

I have the same problem.

Details: jQuery v1.11.3 jQuery validator v1.13.1

//main.js ----
$.validator.setDefaults({
 //options including 'rules', etc.
})
// -----------
//manager.js ----
$('form').validate()
// -----------

But no apply the default settings!

Additional information:

  1. jQuery validator is loaded before main.js
  2. main.js is loaded before manager.js

Thanks in advance for any solution.

jcperez2405 avatar Jun 21 '15 18:06 jcperez2405

Please provide an actual testpage that shows the problem. Code snippets are insufficient. Also, commenting on closed tickets is generally a bad strategy.

jzaefferer avatar Jun 22 '15 09:06 jzaefferer

I noticed the same issue (setDefaults() not working properly). The settings I intended to override were "highlight/unhighlight". My main goal was to make Microsoft javascript unobtrusive validation work with Bootstrap validation styles.

The versions of jQuery and jQuery validation plugin I used:

A. jQuery 1.9.1 + jQuery-Validation 1.8.0 = problem seen B. jQuery 2.1.4 + jQuery-Validation 1.8.0 = problem not seen C. jQuery 2.1.4 + jQuery-Validation 1.13.1 = problem seen

The workaround bresleveloper suggested in his first post solves the issues of case A and C.

mansoor-omrani avatar Jul 23 '15 05:07 mansoor-omrani

Same problem jQuery 2.2.3 + jQuery-Validation 1.15.0 + jQuery.validate.unobtrusive.js

RouR avatar May 23 '16 11:05 RouR

https://jsfiddle.net/3uznxttf/5/

RouR avatar May 23 '16 12:05 RouR

i have the same error,but a solved just calling first jquery and then jquery validator... its weard, but its works.

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>

mrojas18 avatar May 30 '17 16:05 mrojas18

Same problem jQuery 2.2.4 + jQuery-Validation 1.17.0 See RouR's page above - I updated the libraries and it still fails to highlight/unhighlight.

[mrojas18] I'm loading jQuery first and jQuery.validate second.

[bresleveloper] I tried adding this code to the library but it made no change.

murraycollingwood avatar Nov 03 '17 09:11 murraycollingwood

I did not check it further, but as 2 people report the same error we should investigate...

staabm avatar Nov 03 '17 18:11 staabm

Just an update - I had a colleague add the code to the setDefaults() function and it is now working. Not much different. Perhaps a different location in the setup but otherwise nothing that should have impacted this.
Thanks for re-opening however I now have a working 'setDefaults()' function.

murraycollingwood avatar Nov 06 '17 07:11 murraycollingwood

I experienced this issue as well but in my case I had one earlier project working fine and the new one was not working. In both cases I had the same version of jQuery (2.2.0), jquery-validation (1.14.0) and jquery-valdiation-unobtrusive(3.2.6). After a lot of back and forth trying to figure out what was different between these two projects it simply boiled down to the order in which the scripts were loaded.

setDefaults was NOT working when order was:

  1. jQuery
  2. jQuery Validation
  3. jQuery Validation Unobtrusive
  4. My custom script to set the defaults

setDefaults DID work when order was:

  1. jQuery
  2. my custom script to set the defaults
  3. jQuery Validation
  4. jQuery Validation Unobtrusive

To clarify - my custom script that updates the defaults is in a jQuery doc ready and wrapped with an if($.validator){ }. So it won't execute before the validation scripts load, but for whatever reason the exact same code being loaded AFTER the validation scripts load will result in the setDefaults being ignored.

Unfortunately I don't have a code-solution for you, but I did want to add the extra information that may help with a fix.

aaroncoville avatar Nov 24 '17 18:11 aaroncoville

I had a similar experience as @Eltargrim.

The only way I could get it to work was in between jQuery Validation and unobtrusive.

Versions:

  • jQuery | 3.3.1
  • jQuery Validation | 1.17.0
  • jQuery Validation Unobtrusive | 3.2.10

My problem stemmed from #1729. The final comment on that thread is the problem that I am currently having. image

I'm not quite sure how the validation works, but prior to this issue, I assumed it didn't occur until the form submit.

GreenBeandon avatar Jul 10 '18 18:07 GreenBeandon

Thanks @Eltargrim. I think I now understand the problem I'm experiencing. Like most ASP.NET MVC (Core) developers I load my scripts in the following order:

  • jquery.js
  • jquery.validate.js
  • jquery.validate.unobtrusive.js
  • app.js

Unobtrusive validation will do its thing as soon as that script loads and document.ready can run, which is before app.js is loaded and anything in it has ran. So I've updated my code to do the following:

// Inside app.js
$(function () {
    if ($.validator) {
        var ignore = ":hidden, [contenteditable='true']:not([name])";

        // This sets `ignore` for future jquery validate configurations, 
        // but not for unobtrusive validation if it has already been loaded.
        $.validator.setDefaults({
            ignore: ignore
        });

        // Update unobtrusive validation's ignore setting when that script was
        // loaded before this script.
        $('form').each(function () {
            if ($(this).data('validator'))
                $(this).data('validator').settings.ignore = ignore;
        });
    }
});

The $('form').each(function () { … part is necessary because I am loading jquery.validate.unobtrusive.js before app.js, which means unobtrusive has already used whatever default settings were set.

On the otherhand if I loaded my scripts in the following order I would not need the $('form').each(function () { … part because unobtrusive will have the expected defaults that I configure in app.js before it can run.

  • jquery.js
  • jquery.validate.js
  • app.js
  • jquery.validate.unobtrusive.js

jeremycook avatar Jan 11 '19 15:01 jeremycook

Like @jeremycook I believe this issue shows up when using ASP.NET's unobtrusive validation. The unobtrusive validation library calls the validate method passing in options which overrides the defaults.

Therefore I think it can probably be closed.

nfplee avatar Feb 02 '20 00:02 nfplee