Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

Summarizing LoneWolfPRs answer

This would be AND: both checkboxes must be checked to take affect

array(
    ':input[name^="field_checkbox_1"]' => array('checked' => TRUE),
    ':input[name^="field_checkbox_2"]' => array('checked' => TRUE),
)

This would be OR (put the single conditions into arrays): one or both checkboxes must be checked

array(
    array(':input[name^="field_checkbox_1"]' => array('checked' => TRUE)),
    array(':input[name^="field_checkbox_2"]' => array('checked' => TRUE)),
)

This would be XOR (like or and with 'xor' in between the arrays): one and only one checkox must be checked

array(
    array(':input[name^="field_checkbox_1"]' => array('checked' => TRUE)),
    'xor',
    array(':input[name^="field_checkbox_2"]' => array('checked' => TRUE)),
)

Reference to the change record "Fixed conditionals to allow OR and XOR constructions" on d.o.

Since 7.14, Drupal form API #state support OR and XOR conditions.

The following code allows for the condition: dependee_1 OR (dependee_2 OR dependee_3) OR (dependee_4 XOR dependee_5)

<?php
  $form['dependent_1']['#states'] = array(
    'disabled' => array(
    // dependee_1 has value ON
      '[name="dependee_1"]' => array('value' => 'ON'),
      array(
      // At least one of dependee_2 or dependee_3 has value ON
        array('[name="dependee_2"]' => array('value' => 'ON')),
        array('[name="dependee_3"]' => array('value' => 'ON')),
      ),
      array(
      // Only one of dependee_4 or dependee_5 can have value ON
        array('[name="dependee_4"]' => array('value' => 'ON')),
        'xor',
        // The field should be disabled when Select #1 has value ON; at least one
        // of Select #2 and Select #3 has value ON; one but only one of Select #4
        // and Select #5 has value ON.
        array('[name="dependee_5"]' => array('value' => 'ON')),
      ),
    )
  );
?>

Basically all elements in an object literal ({ ... }) are ANDed and all elements in a array literal ([ ... ]) are ORed; if the first element of the array is 'xor' conditions are XORed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top