1

I am having some troubles with ajax on form element checkboxes, in ajax callback form_state is without values and triggering element etc .. why is that?

The form is called with drupal_get_form and this is my element

$form['select_options'] = array(
  '#type' => 'checkboxes',
  '#options' => $options, 
  '#ajax' => array(
    'callback' => 'ajax_options_callback',
    'wrapper' => 'options-form',
    'event' => 'change',
    'method' => 'replace',
  ),
);
  • If $form_state['input'] there? and maybe you have set limit validation errors somehow? – Mołot Aug 23 '13 at 8:17
  • Please also share the code for ajax_options_callback function. – Jayendra Kainthola Aug 23 '13 at 8:33
  • 1
    you haven't used default value this ajax, so when form will postback on ajax this will reset all fields on that page. – Sanjay dev Aug 23 '13 at 8:42
1

When you use a callback $forms and $form_state is available for use in your callback. But you should call them as parameters in your callback definition. So:

public function ajax_option_callback(array $form, FormStateInterface $form_state) {
  // get the id of the form
  $value = $form_state->getFormObject()->getFormId();
  // create a response object and return it
}

public function ajax_option_callback() {
  // $form and $form_state not available
}

Note that &form_state->getTriggeringElement() contains nothing as the form is not submitted.

I realize now the question is for D7. Maybe my answer is usefull for someone within the D8 context.

  • As the author mentioned that the form is called with drupal_get_form. So he is seeking for answer in Drupal 7. – Jimmy Ko Aug 8 '16 at 12:43

Your Answer

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

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