views:

379

answers:

1

I have a very complex form with the validation working correctly. However, since it can take awhile for the validation to complete, I'd like to use blockUI to be called when I click the form's submit button to prevent confusion and double-submissions. I can't quite figure out how to do this.

My code looks like this:

$("#credential").validate({
     rules: {
              EngId: {
                 required: true
                 }
             ClientAccount: {
                 required: true
                 }
              ...
        }

and I'm calling the validation with several buttons (using their click function) depending on selections in the form, often disabling some of the rules:

$("#buttonname").click(function() {
   $("#fieldname").rules("remove");
   ...
   $("#credential").submit();
});

What I can't figure out is where the blockui and unblockui calls would go so that when the user clicks the button, before validation starts, blockui does its magic, and if the validation finds a problem, unblockui is called and enables the form again.

I'm pretty new to Jquery and I can't find any examples that I've been able to implement successfully. I would appreciate any help anyone could give (please excuse if this has been covered before).

A: 
$(document).ready(function() {
    $('#form1').validate({
        rules: {
            fieldone: { required: true },
            fieldtwo: { required: true }
        },
        submitHandler: function(form) {
            $(form).block();
            form.submit();
        }
    });

    $('input:checkbox[name=toggleone]').click(function() {
        if ($(this).is(':checked')) {
            $('input[name=fieldone]').rules('add', { required: true });
        } else {
            $('input[name=fieldone]').rules('remove');
        }
    });

    $('#altsubmit').click(function() {
        $('input[name=fieldtwo]').rules('remove');
        $('#form1').submit();
    });
});

I put together a page to quickly demonstrate a working form with validate, blockui, and dynamic rules. http://zaplabs.com/sandbox/jquery/validateblockui/

h0tw1r3
I tried placing it there, but I have all my rules and message variables in that area as well and that seems to cause a problem. I have no idea where to put that code.
Tim Stewart
Thanks for that edit, but it still doesn't do anything - I added the submitHandler function where you suggested and it doesn't seem to get called at all. I must be doing something else wrong. Thanks for your help though.
Tim Stewart
Use Firefox with the firebug addon and try adding console.log(form) to the submitHandler to check if it's getting called.
h0tw1r3
My code looks like yours in the sense that I have the submitHandler right after the rules and the submit button is calling a click function that calls the $('#form1').submit(); - This all works - the form validates and shows where errors are. However, the blockui is never called - its as if the submitHandler isn't being called at all. I tried adding console.log(form) as suggested and I'm not seeing that it is being called.
Tim Stewart
If you would like to supply your page I could help you identify the problem. Either way, did I answer your original question? Was the example sufficient?
h0tw1r3