If you update the invalidHandler after initialization, the original event handler will always fire. This is because of the way that invalidHandler is set up.
This is in jquery.validate:
if ( this.settings.invalidHandler ) {
$(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
}
The way around this is to update the event handler:
$form.off('invalid-form.validate')
.on('invalid-form.validate', newInvalidHandler);
I propose this change:
if (this.settings.invalidHandler) {
$(this.currentForm).bind("invalid-form.validate", function (event, validator) {
validator.settings.invalidHandler(event, validator);
});
}
Great idea! :) I was having problems with being able to change options for my already initialized validation, but I didn't have and invalidHandler already, I just hacked it in using bind directly, see http://www.herikstad.net/2010/04/add-invalidhandler-after-jquery.html.
As far as I can see, the validate function is for _adding_ new validation to a form. So maybe it would be better to add a separate function for changing the invalidHandler option (something like for rules?). Or, I guess you could flash the whole thing and start anew using the removeData function (as suggested at http://stackoverflow.com/a/13842909/1619146), but that seems like overkill.
Anyways, would be great to have an official way to do this, not just a hack. :)
Thanks for the feedback. To provide a little background, I came upon this issue using Microsoft's jquery.validate.unobtrusive.js included with Mvc. Their script sets up the invalidHandler already on first load of the page, or after you call the method, $.validator.unobtrusive.parse. So because they're already setting it, you cannot set it again.
It seems to me, that this setting should behave just as all the others, allowing you to update it after initialization by passing a new settings object to the validator. However, you would have to pass back the settings using a copy or by using $.extend, so you pass a merging of the old and the new settings.
Maybe better yet, there should be a new validator method, "updateSettings", where you would ony have to pass the setting that you wanted to update.
I'm sorry for the lack of activity on this issue. Instead of leaving it open any longer, I decided to close old issues without trying to address them, to longer give the false impression that it will get addressed eventually.
To the reporter (or anyone else interested in this issue): If you're affected by the same issue, consider opening a new issue, with a testpage that shows the issue. Even better, try to fix the issue yourself, and improve the project by sending a pull request. This may seem daunting at first, but you'll likely learn some useful skills that you can apply elsewhere as well. And you can help keep this project alive. We've documented how to do these things, too. A patch is worth a thousand issues!
See also this Stack Overflow Question: How to add a 'submitHandler' function when using jQuery Unobtrusive Validation? which has the same solution as iamandycohen:
$("form").bind("invalid-form.validate", function () {
alert('invalid!');
});