views:

29

answers:

2

I've got a form that is using jquery validation. When the user clicks the submit button, I've got some jquery that intercepts the submit and should display a dialog window asking the user to verify the information they entered on the form. This dialog window should only display if the form has validated. Here is my code:

$('#form').submit(function(){
    fillVerificationDialog();   /* This loads up the dialog with form data */
    if (('#form').valid()){
        $('verification_dialog').dialog('open');
        return false;
    }
});

My dialog window is not displaying. It just submits the form. And I've verified that the form is valid by using an alert that checks the value of $('#form').valid()

If I remove the if statement and just have the line that opens the dialog, the dialog displays. However, it will display even if the form has errors which is what I'm trying to prevent.

+1  A: 

Make sure you initialize the dialog before trying to open it.

$('#verification_dialog').dialog({ autoOpen: false });

Missing the # in your selector. Since the selector you are looking to use is most likely an id of a div, the dialog will not open, since it's missing the #.

$('#verification_dialog').dialog('open');
gmcalab
Nitpick: That's not an invalid selector.
SLaks
@SLaks - true story, I fixed that.
gmcalab
You're right, I was missing the #, but even with it, the dialog window does not open. The form submits anyway.
Did you initialize the dialog before trying to open it? Check my updated answer.
gmcalab
Yeah, I created a separate function called initVerificationDialog that looks like this: function initVerificationDialog() { //Initialize the verification dialog $('#verification_dialog').dialog({ autoOpen: false, bgiframe: true, resizable: false, modal: true, height: 600, width: 600, buttons: { "Submit Form": function() { document.account.submit(); }, "Cancel": function() { $(this).dialog("close"); } } }); }
My main function lloks like this:My main function looks like this: $(function() { initFormValidation(); initVerificationDialog(); fillVerificationDialog(); $('#form').submit(function() { if (('#form').valid()){ $('#verification_dialog').dialog('open'); return false; } }); });
Maybe just try to initialize it with the simpler form like mine above to rule out any possible syntax issues. Then if that works start adding things back in one at a time.
gmcalab
A: 

Are you sure that you're calling validate on the form before it's being submitting and setting up the dialog?

$('#form').validate();
$('#verification_dialog').dialog({
     autoOpen: false,
     buttons: {
         'OK': function() {
                 ... do form submission...
               },
         'Cancel': function() {
                 $(this).dialog('close');
               }
    }
 });
$('#form').submit(function(){ 
    if (('#form').valid()){
        fillVerificationDialog();   /* This loads up the dialog with form data */ 
        $('#verification_dialog').dialog('open'); 
    } 
    return false;
});
tvanfosson
See above, my main function calls initFormValidation(). This contains all my validation rules and messages. After that, the main function calls initVerificationDialog() which creates the dialog. Then fillVerificationDialog() is called which fills the dialog with data the user entered from the form. Finally, I run the function for the submit button which checks to see if the form is valid.