Overall theses are awesome examples, but one problem that it didn't address is how to skip validations on a rebuild....

You can do it with an AHAH submit button with the "skip_validations" module since you can check for form_state['clicked_button'] and then skip validations only for that time.

With the dropdown since it's not a button... I can't seem to find a way to skip only on the ahah, and not on every other command like preview, and save...
and it isn't even touched upon in the AHAH examples, sure you wouldn't need it for single items.... but when you use the examples on your own form, validation bugs become a pain in the ass....

So any help would be appreciated! (especially skipping validations for dependent_dropdown !)

Comments

Swayt’s picture

Status: Active » Needs work
rfay’s picture

Title: Missing Skip Validations in AHAH Examples » Missing submit handling and validation (skipping?) in AHAH Examples
Status: Needs work » Active

Glad you brought this up - We need to show submit handling with $form_state['rebuild'] = TRUE as well.

I don't think that in D6 you can skip validation - In D7 the #limit_validation_errors property was added for this.

Swayt’s picture

Status: Needs review » Active

Well here's a small fix for disabling validations, after going through the AHAH helper module I managed to make a quick tweak that works. :)

ALL you need is the, skip_validations module and use the function "_skip_validation_skip_validation($form, 'full');"
to skip all validations on the AHAH components, and not affect the whole form.

function metadata_research_ahah() {
  include_once drupal_get_path('module', 'node') . '/node.pages.inc';
  $form_state = array('storage' => NULL, 'submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);

  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form_state['post'] = $form['#post'] = $_POST;
  $form['#programmed'] = $form['#redirect'] = FALSE;

  //Skips validations for the AHAH submit values, put it in your callback function.
  _skip_validation_skip_validation($form, 'full');
  
  drupal_process_form($form_id, $form, $form_state);
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  $changed_elements = $form['groups']['research_wrapper'];
  unset($changed_elements['#prefix'], $changed_elements['#suffix']); // Prevent duplicate wrappers.
  drupal_json(array(
    'status'   => TRUE,
    'data'     => theme('status_messages') . drupal_render($changed_elements),
  ));
}

You could write your own function to skip validation and not have to worry about using another module.


//quick example from AHAH_helper module, doesn't work for hook_validate validations tho, just the required.

function _ahah_helper_disable_validation(&$form) {
  foreach (element_children($form) as $child) {
    $form[$child]['#validated'] = TRUE;
    _ahah_helper_disable_validation(&$form[$child]);
  }
}
Swayt’s picture

Status: Active » Needs review
rfay’s picture

Thanks for your work on this.

However, to put it in "Needs Review" you need to attach a patch. I haven't fully studied your approach yet. If we put it in, it will probably be an additional example.

rfay’s picture

Status: Active » Needs review
StatusFileSize
new6.22 KB

I added a simple $form_state['ahah_submission'] into the ahah helper callback; that can now be checked in submit or validation handler.

rfay’s picture

Status: Needs review » Fixed

Committed to DRUPAL-6-1, http://drupal.org/cvs?commit=425802

Thanks so much, Swayt. Would appreciate your comment on this approach.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Floris248’s picture

Swayt you need an award for that post, seriously.

Thank you.

yeaha’s picture

thank you!

davepoon’s picture

Thank you so much! That's really useful!!

omar alahmed’s picture

Thank you,

flamingvan’s picture

I discovered a pretty simple way to suppress form validation without using any contrib modules:

within your form function (the one where you specify fields)

if($form_state['ahah_submission']){
    // Clear the form errors.  We don't need to show any validation errors if this is an ahah submit, not a final submit
    drupal_get_messages('error');
    // Clear the form error state.
    form_set_error(null, '', true); 
}
Madis’s picture

Thanks, seems to work nicely!

Tim_MA’s picture

Thanks flamingvan works an absolute treat, good work!

Fergie’s picture

Flamingvan, thank you, man! I've been struggling with this for hours!