7

Using #states to change form element visibility, I would like a 2nd element to be visible only if the 1st element is filled in. The first element is a pair of radio buttons, so I tried:

  '#states' => array('visible' => array(
  ':input[name="radio-id"]' => array('value' => '0', 'value' => '1'),
    ),),

However that doesn't work, the 2nd value condition clobbers the first, so the other element only appears if the 2nd radio ('1') is checked. So then I tried:

    :input[name="radio-id"]' => array('checked' => TRUE),

Nope, that doesn't work either.

What is the correct syntax to use for this, and which documentation page is it on? (drupal.org or other site?) I've looked and looked but can't find it.

3 Answers 3

9

Had same problem today. A slight modification of the negated value expression proposed on this comment: "Empty Radio Selection" on drupal_process_states() API page, did the trick for me!

'#states' => array(
   'visible' => array(
     ':input[name="radios-name"]' => array('!value' => false),
   ),
 ),

I just wanted the select list with the above #states entry to be invisible while no selection has still been made on my list of five radios (sharing name "radios-name").

Radios were also '#required' => true, in my case and all 5 individual radio-buttons have non-numeric string values. No #default_value is specified in my case either.

Disclaimer: I have only tested this on Chrome!

2
  • Excellent, thanks. Works on Firefox too, by the way. May 20, 2014 at 8:11
  • Thanks - works well. Note in the example you link to they are using the invisible state as opposed to visible that you are using. I also have to use invisible to get it to work.
    – Felix Eve
    Dec 17, 2014 at 21:47
2

You should take a look at the States example in the Form Example in the Examples project. This exact situation is demonstrated several different ways.

1
  • Please try to post code snippet here. We want our users here as long as possible.
    – niksmac
    Nov 26, 2012 at 5:38
2

The documentation for the #states property is in the documentation for drupal_process_states(), which contains the following sentence and example code.

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'),
    ),
  ),

The code you are using in your second snippet works for checkboxes, such as in the following example.

$form['toggle_me'] = array(
  '#type' => 'checkbox',
  '#title' => t('Tick this box to type'),
);

$form['settings'] = array(
  '#type' => 'textfield',
  '#states' => array(
    // Only show this field when the 'toggle_me' checkbox is enabled.
    'visible' => array(
      ':input[name="toggle_me"]' => array('checked' => TRUE),
    ),
  ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.