4

Form

$form['animal'] = array(
    '#type' => 'select',
    '#title' => t('Animal'),
    '#options' => load_animals(),
    '#ahah' => array(
        'event' => 'change',
        'path'  => 'path/to/ajax/service',
        'method' => 'replace',
        'effect' => 'fade',
        'wrapper' => 'breed-wrapper',
    ),
);
...
$form['breed'] = array(
    '#type'     => 'select',
    '#title'    => t('Breeds'),
    '#options'  => array('Select animal to load breed'),
    '#prefix'   => '<div id="breed-wrapper">',
    '#suffix'   => '</div>',
);

And following is the AHAH callback processing

$post = $_POST;
$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['#redirect'] = FALSE;
$form['#post'] = $post;
$form['#programmed'] = FALSE;
$form_state['post'] = $post;
drupal_process_form($form_id, $form, $form_state);
// New form elements
$breed_form = $form['breed'];
$options = load_breeds((int)$post['animal']);
$breed_form['#options'] = $options;
$form['breed'] = $breed_form;
form_set_cache($form_build_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

unset($breed_form['#prefix'], $breed_form['#suffix']);
// Render the new output.   
$output .= drupal_render($breed_form);

drupal_json(array('status' => TRUE, 'data' => $output));

Default form submit handler

function default_form_submit(&$form, $form_state){
    $clicked_button = $form_state['clicked_button']['#value'];
    $values = $form_state['values'];
    if($clicked_button == $values['submit']){
        unset($values['op'], $values['submit'], $values['form_build_id'],
            $values['form_token'], $values['form_id']);
        ....
        drupal_goto($_REQUEST['q'], $query);
    }
}
  • When I finally submit the form in normal post way, a validation error is reported as An illegal choice has been detected. Am I properly using form_set_cache()?

  • On AHAH post, the default form submission handler is also invoked. As this handler contains a redirection logic so AHAH request is collapsed. How to bypass it even-though I am doing click_button validation?

Shoaib Nawaz
  • 2,302
  • 4
  • 29
  • 38
  • I am not very much sure but I think that this article would help you a lot. http://www.mindyourcode.com/php/adding-dynamic-form-elements-using-ahah-in-apply-for-for-role-module/ – Mashhadi Mar 07 '13 at 12:46

1 Answers1

0

i think for your last question, use need to set $form_state['ahah_submission'] = TRUE.

au_stan
  • 4,011
  • 2
  • 19
  • 24