Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have many AJAX forms on the page and on button click I need to submit them all. Regular forms.each(function (index, form) { $(form).submit();} won't work for me because in this way ONLY the last form will be submitted eventually. Therefore, I need submit them via $.ajax(...). But I want to enable submit ONLY and ONLY if form is VALID


On "submitForms" click the fucntion "submitTest" is invoked.
From jQuery documentation "submitHandler" would be called if form is VALID.
So, the validation works and "submitHandler" is NOT invoked if form is INVALID. BUT, the XmlHttpRequest is sent anyway, though "submitHandler" isn't invoked.

Where I make mistake?
Thank you

<% using (Ajax.BeginForm("FormPost", "Customer", null, new AjaxOptions() { HttpMethod = "POST" }, new { @class = "form-container" }))
           { %>
        
                <%: Html.TextAreaFor(m => m.Name, new { width = 440, height = 100 })%>
                 <input type="button" value="submitForms" />
        <% }%>

<script type="text/javascript">
function submitTest()
{
    var forms = $(".form-container");
    
    forms.each(function (index, form) {
        $(form).validate({
             submitHandler: function (form) {
                     $.ajax(
                     { 
                        ....
                     });
             }
        });
    });

    forms.each(function (index, form) {
         $(form).submit();
    });
}
</script>
Posted
Updated 12-Jan-11 22:47pm
v3

1 solution

Please see my comments to your code:

C#
<script type="text/javascript">
function submitTest()
{
    var forms = $(".form-container");
    
    // Here you setup the validation for your forms and all you are
    // doing here is installing an submit handler that will get fired
    // after successful validation, but there are no validation rules.
    forms.each(function (index, form) {
        $(form).validate({
             // This handler will be called for every form because you
             // forgot to specify validation rules so the validation will
             // never fail.
             submitHandler: function (form) {
                     $.ajax(
                     { 
                        ....
                     });
             }
        });
    });

    // Here you trigger the submit event for each form. This will cause
    // the validation of each of the forms. Since no validation rules
    // were setup each form will validate just fine. This means the
    // jQuery Validation plugin will call the submitHandler function you
    // installed when setting up the form validation and the AJAX call
    // will be made.
    forms.each(function (index, form) {
         $(form).submit();
    });
}
</script>


There are two things you can do:
1. Setup validation rules for the forms. See the documentation of the Validation plugin for jQuery on how to do that.
2. Make the nescessary checks inside the submitHandler function. If the checks succeed you make the AJAX request else not. This kind of defeats the presence of the Validation plugin though.

My advice is to take 1. as the way to this.

Caveat: Setting up the same validation rules for all forms like you did in your code example also means that all forms would have identical fields (at least the ones that are validated). This can make sense in some scenarios, but it is also possible that this is not what you wanted.

If you have any doubts, leave a comment to my answer.

Best Regards,
Manfred
 
Share this answer
 
v3
Comments
Espen Harlinn 14-Jan-11 5:59am    
5+ You are thoroughly raising the bar on answers Manfred
Manfred Rudolf Bihy 14-Jan-11 6:03am    
Thanks for the compliment Espen.
TheAteist 16-Jan-11 2:54am    
Thank you for your explanation, BUT the rules are added automatically by calling to Html.EnableClientValidation(), (I use DataAnnotation)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900