title: Drupal AHAH, Dynamic form expansion
body: <p>Form</p>
  
  <pre><code>$form['animal'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; t('Animal'),
      '#options' =&gt; load_animals(),
      '#ahah' =&gt; array(
          'event' =&gt; 'change',
          'path'  =&gt; 'path/to/ajax/service',
          'method' =&gt; 'replace',
          'effect' =&gt; 'fade',
          'wrapper' =&gt; 'breed-wrapper',
      ),
  );
  ...
  $form['breed'] = array(
      '#type'     =&gt; 'select',
      '#title'    =&gt; t('Breeds'),
      '#options'  =&gt; array('Select animal to load breed'),
      '#prefix'   =&gt; '&lt;div id="breed-wrapper"&gt;',
      '#suffix'   =&gt; '&lt;/div&gt;',
  );
  </code></pre>
  
  <p>And following is the AHAH callback processing</p>
  
  <pre><code>$post = $_POST;
  $form_state = array('storage' =&gt; NULL, 'submitted' =&gt; 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' =&gt; TRUE, 'data' =&gt; $output));
  </code></pre>
  
  <p>Default form submit handler</p>
  
  <pre><code>function default_form_submit(&amp;$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);
      }
  }
  </code></pre>
  
  <ul>
  <li><p>When I finally submit the form in normal post way, a validation error is reported as <code>An illegal choice has been detected.</code> Am I properly using form_set_cache()? </p></li>
  <li><p>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?</p></li>
  </ul>