Created
May 29, 2013 14:02
-
-
Save danielgreen/5670480 to your computer and use it in GitHub Desktop.
When using jQuery Validate, you may want to run some code when it detects that the form is invalid. When using Unobtrusive Validation, you don't have the ability to set invalidHandler directly when initialising the Validate plugin, so here is a workaround.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If using Unobtrusive Validation then that library initialised the Validate plugin | |
// on our behalf, so we missed the chance to set invalidHandler. Even if we were to | |
// set invalidHandler now via the settings object it would have no effect, due to the | |
// way that Validate works internally. Instead, we can do the following: | |
$("#MyForm").bind("invalid-form.validate", function () { | |
// Do something useful e.g. display the Validation Summary in a popup dialog | |
$("div.ValSummary").dialog({ | |
title: "Information", | |
modal: true, | |
resizable: false, | |
close: function (event, ui) { | |
$(event.target).remove().appendTo("#MyForm"); | |
}, | |
buttons: { | |
"OK": function () { $(this).dialog("close"); } | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, except it fires twice for me.