Drupal’s Form API helps developers build complex, extendable user input forms with minimal code. For most common Form UI tasks, the #states system eliminates the need to write custom JavaScript. It eliminates inconsistencies by translating simple form element properties into standardized JavaScript code.

The following states may be applied to an element:

– enabled
– disabled
– required
– optional
– visible
– invisible
– checked
– unchecked
– expanded
– collapsed

The following states may be used in remote conditions:

The following states may be used in remote conditions:
– empty
– filled
– checked
– unchecked
– expanded
– collapsed
– value

The following states exist for both elements and remote conditions, but are not fully implemented and may not change anything on the element:

– relevant
– irrelevant
– valid
– invalid
– touched
– untouched
– readwrite
– readonly

When referencing select lists and radio buttons in remote conditions, a ‘value’ condition must be used:

'#states' => array(
   // Show the settings if 'bar' has been selected for 'foo'.
   'visible' => array(
     ':input[name="foo"]' => array('value' => 'bar'),
   ),
 ),

Example:

$form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#states' => array(
    'invisible' => array(
      ':input[name="anonymous"]' => array('checked' => TRUE),
    ),
  ),
);
$form['anonymous'] = array(
  '#type' => 'checkbox',
  '#title' => t('I prefer to remain anonymous'),
  '#states' => array(
    'unchecked' => array(
      ':input[name="name"]' => array('filled' => TRUE),
    ),
  ),
);
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('Email'),
  '#states' => array(
    'visible' => array(
      ':input[name="name"]' => array('filled' => TRUE),
      ':select[name="method"]' => array('value' => 'email'),
    ),
  ),
);
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('Email'),
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="name"]' => array('filled' => TRUE),
        ':select[name="method"]' => array('value' => 'email'),
      ),
      array(
        ':input[name="anonymous"]' => array('checked' => TRUE),
        ':select[name="method"]' => array('value' => 'email'),
      ),
    ),
  ),
);
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('Email'),
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="name"]' => array('filled' => TRUE),
        ':select[name="method"]' => array('value' => 'email'),
      ),
      'xor',
      array(
        ':input[name="anonymous"]' => array('checked' => TRUE),
        ':select[name="method"]' => array('value' => 'email'),
      ),
    ),
  ),
);