setDefaults not working + suggested solution
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
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.
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
This is actually working fine with the latest release.
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:
- jQuery validator is loaded before main.js
- main.js is loaded before manager.js
Thanks in advance for any solution.
Please provide an actual testpage that shows the problem. Code snippets are insufficient. Also, commenting on closed tickets is generally a bad strategy.
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.
Same problem jQuery 2.2.3 + jQuery-Validation 1.15.0 + jQuery.validate.unobtrusive.js
https://jsfiddle.net/3uznxttf/5/
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>
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.
I did not check it further, but as 2 people report the same error we should investigate...
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.
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:
- jQuery
- jQuery Validation
- jQuery Validation Unobtrusive
- My custom script to set the defaults
setDefaults DID work when order was:
- jQuery
- my custom script to set the defaults
- jQuery Validation
- 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.
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.

I'm not quite sure how the validation works, but prior to this issue, I assumed it didn't occur until the form submit.
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
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.