Changing form field requirement via ajax 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' =&gt; 'ajax_type_callback',
  'event' =&gt; '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?
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' =&gt; 'ajax_type_callback',
  'event' =&gt; 'change',
  'wrapper' =&gt; 'version-release'
);
$form['field_version']['#prefix'] = '&lt;div id="version-release"&gt;';
$form['field_version']['#suffix'] = '&lt;/div&gt;';
$typeValueChangedByUser = $form_state-&gt;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'];
}