0

I have a form which when I click on on radio rebuild an element of the same form. Somehow when the ajax is called it's recalling everything from my form function. How can I force him to reload only a part of the code, in that case the $form['container']['network'] and not the whole $form.

There must be some strange behavior during the callback, could it be reloading the whole form and get the part it wants then replace the old by the new?

$form['container']['ZoneId']['#ajax'] = array(
    'callback' => 'change_countries',
    'wrapper' => 'networks-list',
    'method' => 'replace',
    'effect' => 'fade',
);

$form['container']['network'] = array(
    '#type' => 'fieldset',
    '#prefix' => t('<div id="networks-list" class="checkbox-styling">'),
    '#suffix' => '</div>',
);

function change_countries($form, $form_state) {
    $form_state['rebuild'] = TRUE;
    return $form['container']['network'];
}

1 Answer 1

1

You shouldn't need:

$form_state['rebuild'] = TRUE;

Otherwise your callback looks correct.

5
  • There must be some strange behavior during the callback, could it be reloading the whole form and get the part it wants then replace the old by the new?
    – GwenM
    Commented Jan 7, 2016 at 12:00
  • What happens? Does the part of the form you want to replace get replaced by the whole form? Commented Jan 7, 2016 at 12:18
  • no but it reload all the functions I called in that same form. It displays the correct block at the right place.
    – GwenM
    Commented Jan 7, 2016 at 13:00
  • 1
    Do you mean it calls your form building function before it calls the callback? If so, this is normal behaviour, as the form does not persist between browser requests, so needs to be rebuilt before Ajax callbacks or form submits can be handled. If you do not want functions to be called in the form rebuild, you'll need to put conditional logic in the form function to handle this. Commented Jan 7, 2016 at 13:49
  • That was it! I didn't expect drupal to reload the whole form function during a ajax call.
    – GwenM
    Commented Jan 8, 2016 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.