2

I need to disable field A if field B is disabled too. Actually, my current field A has the following #states (altered using hook_alter_form) to disable it if field B or field C are empty:

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, $form_state, $form_id) {
 
  $form['field_a']['#states'] = [
    'disabled' => [
      [':input[name="field_b[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_c[0][value][date]"]' => ['empty' => TRUE]],
    ],
  ];
}

And it works perfectly, but I need to add the condition mentioned before. I found this in the official documentation: https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields and I tried to use some of them like 'readonly' or 'readwrite' but this didn't work. Can I evaluate properties like '#disabled' from here? something similar to this:

function my_module_form_alter(&$form, $form_state, $form_id) {

  $form['field_a']['#states'] = [
    'disabled' => [
      [':input[name="field_b[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_c[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_b"]' => ['#disabled' => TRUE]],
    ],
  ];
}
1

1 Answer 1

0

You can't do this with states here is what the docs says

The following states may be used when checking values of other fields:

empty
filled
checked
unchecked
expanded
collapsed
value

So, for this particular feature you will need to use some javascript or jquery.

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.