0

I have a selectbox field, that should change the disabled and required states of another field based on the value selected in the selectbox field. The change must be done via ajax.

In my modules form_alter function I have the following code:

$form['field_type_app_details']['widget']['#ajax'] = array(
  'callback' => 'ajax_type_callback',
  'event' => 'change'
);
if ($form['field_type_app_details']['widget']["#default_value"][0] === 'First') {
  unset($form['field_version']['#disabled']);
  $form['field_version']['widget'][0]['value']['#required'] = true;
} else {
  $form['field_version']['#disabled'] = 'disabled';
  $form['field_version']['widget'][0]['value']['#required'] = false;
}

And the corresponding ajax_type_callback function looks like this:

function ajax_type_callback($form, $form_state) {
  return $form['field_type_app_details'];
}

What I would expect is, that when I change the value in the selectbox, to i.e. "Second", that the required and disabled states of field_version change accordingly.

What actually happens is, that when I change the value in the selectbox, the ajax request fires correctly, but doesn't change anything of field_version. Additionally, not even the changed value of field_version is saved, when the form gets submitted.

Where is the mistake?

  • 1
    You should be testing $form_state->getValue(['field_type_app_details', 'widget', 0, 'value']) or similar – Clive Nov 23 '18 at 10:17
  • @Clive Thanks for the input, but what is the difference between the two variants? – plocks Nov 23 '18 at 10:25
  • @Clive I've tried your recommendation, but that just gets me NULL instead of the actual value set in the field, which is "first" at this moment. – plocks Nov 23 '18 at 10:30
  • Yeah it was just an example, if you inspect what's in $form_state->getValues() you should be able to work out the exact keys you need. The main point is to get that value from the $form_state, not from the $form, which is the original form build and doesn't contain any user input – Clive Nov 23 '18 at 10:33
  • @Clive Thanks for the clarification, I'll try that. – plocks Nov 23 '18 at 10:36
0

Thanks to @Clive for pointing me in the right direction.

I've managed to accomplish what I want with the following changes to the form_alter function:

$form['field_type_app_details']['widget']['#ajax'] = array(
  'callback' => 'ajax_type_callback',
  'event' => 'change',
  'wrapper' => 'version-release'
);
$form['field_version']['#prefix'] = '<div id="version-release">';
$form['field_version']['#suffix'] = '</div>';
$typeValueChangedByUser = $form_state->getValue(['field_type_app_details', 0, 'value']);
$typeToCheck = $typeValueChangedByUser ? $typeValueChangedByUser : $form['field_type_app_details']['widget']['#default_value'][0];
if ($typeToCheck  === 'First') {
  unset($form['field_version']['#disabled']);
  $form['field_version']['widget'][0]['value']['#required'] = true;
} else {
  $form['field_version']['#disabled'] = 'disabled';
  $form['field_version']['widget'][0]['value']['#required'] = false;
}

And the following change to the ajax_callback function:

function ajax_type_callback($form, $form_state) {
  return $form['field_version'];
}

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.