Preventing Block Add/Edit Dialog submitting with jQuery

Permalink
I am trying to do some jquery validation of a block add/edit dialog and prevent the dialog submitting if validation fails.

My code boils down to:

// check add/edit dialog on submit
$('#div_in_my_edit_dialog').closest('form').submit(function(ev){
  if (validate_ok()){
    return true;
  } else {
    ev.preventDefault();
    return false;
  }
});


The submit handler is firing (checked it with alert() inserted in various places), my code is running, but the form is getting submitted regardless of whether validation passes or not.

Similar code has worked in many other situations, but this is the first time I have done this in a C5 block add/edit dialog.

Has anyone experienced similar?

Are there any other ways to prevent a C5 add/edit dialog from submitting?

Is there a bug?

JohntheFish
 
aghouseh replied on at Permalink Reply
aghouseh
C5 has its own validation callback that you can hook into via the auto.js file (that sits adjacent to your edit/add.php). The external form block has a really basic example of this.

ccmValidateBlockForm = function() {
   if ($("#cstFilename").val() == '') { 
      ccm_addError(ccm_t('form-required'));
   }
   return false;
}


You can also configure the strings that it returns in the block controller.
public function getJavaScriptStrings() {
   return array('form-required' => t('You must select a form.'));   
}
JohntheFish replied on at Permalink Reply
JohntheFish
In the end I opted for intercepting the click event for the dialog submit button (which is actually a link styled as a button). It gave me more control over how I processed and reacted to the dialog content.