1

Trying to understand the default ajax behavior on drupal forms. Let's say I have a form builder function that creates few textfields and a submit button. Before form elements are generated I'm running few functions that call the database and feed some data that decides which fields are displayed, which are not. And let's say that submit button has an ajax callback that is supposed to add another field to the form depending on the value from one of the fields.

Is ajax really just injecting the new field into the 'wrapper' or is it reloading the whole form (which means it also repeats the database call?)

Example

function mymodule_form($form, &$form_state) {
    $result = do_some_database_stuff();

    $form['myform']['field1'] = array(
      '#type' => 'textfield', 
      '#title'  => 'field 1',
    );

    $form['myform']['field2'] = array(
      '#type' => 'textfield', 
      '#title'  => 'field 2',
    );

    $form['myform']['field3'] = array(
      '#type' => 'textfield', 
      '#title'  => 'field 3',
    );

    if($result == something){
       $form['myform']['field4'] = array(
         '#type' => 'textfield', 
         '#title'  => 'field 4',
       );
    }

    $form['myform']['fieldset'] = array(
      '#prefix' => '<div id="fieldset">',
      '#suffix' => '</div>',
      '#type' => 'container',
    );

    $form['myform']['submit'] = array(
      '#type' => 'submit', 
      '#title'  => 'Press me',
      '#ajax' => array(
        'callback' => 'ajax_callback',
        'wrapper' => 'fieldset',
        'method' => 'replace',
      ),
       '#submit' => array('myform_submit'),
    );
}

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

function ajax_callback($form, $form_state){
  if($form_state['values']['field1'] == true){
    $form['myform']['fieldset']['newfield'] = array(
      '#type' => 'textfield',
      '#name' => 'New Field',
    );
  }
  return $form['myform']['fieldset'];
}
  • It loads the form from the cache, which is most often in the database. And, of course, the form has to be re-built (otherwise the cache won't contain the new/altered element(s)) so your form function, and anything it calls, will be run again – Clive Jun 19 '14 at 10:32
  • So (unless I store $result in $form['storage'] and add specific condition checks) $result = do_some_database_stuff(); would be fired every time ajax call is made, is that correct? – belinea Jun 19 '14 at 10:34
  • Correct - but don't forget, your form state is passed around by reference - you already have your condition: if($form_state['values']['field1'] == true){, just negate that to stop your db call running twice. BTW you don't have to use $form['storage'], you might be thinking of the old $form_state['storage'] from Drupal 6? In D7 $form_state is a playground, you don't specifically need to use storage – Clive Jun 19 '14 at 10:36
  • 1
    yeah, sorry, I know that. I just got used to calling it storage :) Ok, thanks for explanation – belinea Jun 19 '14 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.