I'm trying to capture the jquery validate plugin error callback. I understand you can easily hook into this if I was calling validate directly:
$('#form').validate({
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
//etc
}
}
});
But I'm not. Instead, my form is submitted to server and if !Model.IsValid
then the server returns the invalid model and populates the place holder with the error message:
<span data-valmsg-replace="true" data-valmsg-for="Email" class="field-validation-error">This field is required</span>
I need to know when this span gets updated with the error message because I have the following wrapper for it:
<div class="errorContainer">
@Html.ValidationMessageFor(m => m.Email) /*This is the span above*/
</div>
Where div.errorContainer { visibility : hidden; }
and I need to make it visible only when there is an error message (because it has a background and does not belong unless there is an error inside).
How can I do this? The optimal solution would be where I can respond to the error from BOTH client and server side validation because I plan to build in the client-side validation after I make sure the server side is covered. I guess the better question then is, is there a way to have the invalidHandler
callback fire from server returned errors?