3
votes

I would like to wire up ALL forms so that a loader is shown after Jquery Validation has found no errors and before the form is submitted or on submit.

How can this be accomplished using MVC 4 unobtrusive validation?

I have tried the following as per this article, How to add a 'submitHandler' function when using jQuery Unobtrusive Validation?:

$("form").data("validator").settings.submitHandler = function (form) { alert('submit'); form.submit(); };

I placed this in the OnLoad section but this does not fire. I asume the above will work for all forms?

Thanks!

1
Nothing. I was hoping there was some kind of callback that could be made. - Julian Dormon

1 Answers

6
votes

See the documentation for the built-in setDefaults method. You also do not need the form.submit() line as the plugin already takes care of that.

$.validator.setDefaults({
    submitHandler: function (form) {
        alert('valid form submitted'); // for demo
        return false; // for demo, blocks default submit, needed with ajax too.
    }
});

DEMO: http://jsfiddle.net/W4jfY/


EDIT:

To apply to just one form instead...

$('#myform').validate({
    // any other options for this form,
    submitHandler: function (form) {
        alert('valid form submitted'); // for demo
        return false; // for demo, blocks default submit, needed with ajax too.
    }
});