Greetings!
I have a tree of form elements, where root group has #tree = TRUE
When I use #states to hide elements conditionally in this tree, all the hidden form elements are showing.

If I remove #tree = TRUE , the form works fine.

I use #tree to collect all settings in a single array, using system_settings_form().

Any idea why?

Comments

Daniel_KM’s picture

Hi,

"#states" uses Javascript. If you use a tree, the form item has another name, that you should use in your form item.

If you have this form without tree:

  $form['protocol'] = array(
    '#title' => t('Protocol'),
    '#type' => 'select',
    '#default_value' => 'none',
    '#options' => array(
      'none' => t('None'),
      'json-rpc' => t('JSON-RPC'),
    ),
    '#required' => TRUE,
  );

  $form['url'] = array(
    '#title' => t('Full url'),
    '#type' => 'textfield',
    '#default_value' => '',
    '#size' => 50,
    '#maxlength' => 256,
    '#required' => TRUE,
    '#states' => array(
      'invisible' => array(
        ':input[name=protocol]' => array('value' => 'none'),
      ),
    ),
  );

You can convert it to :

  $form['data']['#tree'] = TRUE;

  $form['data']['protocol'] = array(
    '#title' => t('Protocol'),
    '#type' => 'select',
    '#default_value' => 'none',
    '#options' => array(
      'none' => t('None'),
      'json-rpc' => t('JSON-RPC'),
    ),
    '#required' => TRUE,
  );

  $form['data']['url'] = array(
    '#title' => t('Full url'),
    '#type' => 'textfield',
    '#default_value' => '',
    '#size' => 50,
    '#maxlength' => 256,
    '#required' => TRUE,
    '#states' => array(
      'invisible' => array(
        ':input[name="data[protocol]"]' => array('value' => 'none'),
      ),
    ),
  );

You can use alse another reference, as ':input[id=edit-data-protocol]' => array('value' => 'local'),.

Warning, you need to protect your #states item with ", because there are "[" and "]".

Sincerely,

Daniel Berthereau
Infodoc & Knowledge management

farald’s picture

Status: Active » Closed (works as designed)

Thank you lots for this clarification Daniel!

jtellis’s picture

Issue summary: View changes

Thank you! I've been beating my head against this problem for far too long!