Search code examples
drupal-7drupal-modulesdrupal-forms

Drupal 7 Form API: Set visible state of form element based on OR instead of AND


I've got a fieldset that I'd like to show up only if a select box has one of a few options selected. The problem is, I can't seem to see how to work an OR. If you include multiple conditions in the array it operates on like an AND. I need it to work so that if the select box has value of 1 2 or 3 for example.


Solution

  • Apparently as of drupal 7.14 'or' and 'xor' are supported. It's just not in the documentation any where easily found. Here's what I did as an example in case anyone needs it. This works.

    $form['survey'] = array(
        '#type' => 'fieldset',
        '#collapsible' => FALSE,
        '#states' => array(
            'visible' => array(
                array(
                    array(':input[name="measurementmethod"]' => array('value'=>'5')),
                    'xor',
                    array(':input[name="measurementmethod"]' => array('value'=>'6')),
                    'xor',
                    array(':input[name="measurementmethod"]' => array('value'=>'7'))
                )
            )
        )
    );