The drop is always movingYou know that saying about standing on the shoulders of giants? Drupal is standing on a huge pile of midgetsAll content management systems suck, Drupal just happens to suck less.Popular open source software is more secure than unpopular open source software, because insecure software becomes unpopular fast. [That doesn't happen for proprietary software.]Drupal makes sandwiches happen.There is a module for that

I fought AJAX and I won

Submitted by nk on Fri, 2011-04-08 10:58

What I wanted, stupid me, change a select box, get my node form to rebuild on an "AJAX submit" and display some changed elements. Well, AJAX does not fire form level submit handlers (this is supposedly a good thing). But, the form only rebuilds if the "rebuild" flag gets set ($form_state['rebuild']) by a submit handler and a submit handler only -- which are not fired. Opsie! While a custom form surely can do something in #process, #after_build etc a node form really wants to be rebuilt. Seems like a stalemate. If it would be a button, then the button level submit handler would help. Well:

    $form['some_select_box'][LANGUAGE_NONE] += array(
      '#ajax' => array(
        'callback' => 'my_ajax_callback',
        'wrapper' => 'some-wrapper',
      ),
      '#limit_validation_errors' => array(array('some_select_box')),
      '#executes_submit_callback' => TRUE,
      '#submit' => array('my_submit_handler')
    );

function my_submit_handler($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

now there we are. The select box now is a submit button too!! AJAX will set the select box as the trigger element ($form_state['triggering element']) and as such this submit handler will fire, giving us a most excellent chance for a $form_state['rebuild'] = TRUE; and life is now good. While I think AJAX is fundamentally broken in D7 because of this, I have Form API and I am not afraid to use it.

Ps. make sure your radios have valid #default_value otherwise even limit_validation_errors won't help you.

Commenting on this Story is closed.

Submitted by whitexbrian on Fri, 2011-06-24 06:31.

That was pretty interesting