Drupal AHAH, dynamic form expansion

The 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>', ); 

The 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 submission 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 a regular mailbox, the validation error is indicated as An illegal choice has been detected. Am I using form_set_cache () correctly?

  • The default form submission handler is also called on the AHAH post. Because this handler contains redirection logic, the AHAH request is crashing. How to get around it even - although I do the check with the click of a mouse button?

+4
source share
1 answer

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

0
source

Source: https://habr.com/ru/post/1346790/


All Articles