Doc : https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields
States that can be applied to a form field element:
- enabled
- disabled
- required
- optional
- visible
- invisible
- checked
- unchecked
- expanded
- collapsed
The following states may be used when checking values of other fields:
- empty
- filled
- checked
- unchecked
- expanded
- collapsed
- value
The following states exist for both elements and remote conditions.
Those might not be implemented in all browsers and may not change anything on the element:
- relevant
- irrelevant
- valid
- invalid
- touched
- untouched
- readwrite
- readonly
Examples :
Disable submit button until email field is empty
$form['submit'] = [
'#type' => 'submit',
'#states' => [
'disabled' => [':input[name="email"]' => ['value' => ''],]
]
];
OR, AND, XOR Conditions
'disabled' => [
[':input[name="email"]' => ['value' => ''],],
'and',
[':input[name="zip_code"]' => ['value' => ''],],
]
Comments1
Not working for file fields
As the title says, this is working for file fields : Notice: Undefined index: #type in Drupal\Core\Form\FormHelper::processStates() (line 211 of core\lib\Drupal\Core\Form\FormHelper.php).