Conditional Fields with Form API

Public

Show field two only when the user selects field one to be 'No'

</> CopyGet raw version
php
  1. $options = array(0 => t('Yes'), 1 => t('No'));
  2. $form['field_one'] = array(
  3. '#type' => 'radios',
  4. '#title' => t('Field One Title'),
  5. '#default_value' => 0,
  6. '#options' => $options,
  7. '#description' => t('Description'),
  8. );
  9.  
  10. $form['field_two'] = array(
  11. '#type' => 'textfield',
  12. '#title' => t('Field Two Title'),
  13. '#description' => t('Field description'),
  14. '#states' => array(
  15. 'visible' => array(
  16. ':input[name="field_one"]' => array('value' => '1'),
  17. ),
  18. ),
  19. );