Today's Deals (in Amazon.com)

 Question:

Drupal form #states using OR
  • user13134

    I am trying to make a part of my form display depending on the state of a select box. The select box code is:

    $form['product_type'] = array(
        '#type' => 'select',
        '#title' => t('Product Type'),
        '#options' => array(
          'a' => t('a'),
          'b' => t('b'),
          'c' => t('c'),
        ),
        '#required' => TRUE,
      );
    

    I then have a field when I only wan to show it when the value is b or c.

    $form['problem'] = array(
        '#type' => 'textarea',
        '#title' => t('Problem'),
        '#states' => array(
          'visible' => array(
            array(
               ':input[name="product_type"]'=> array('value' => 'b'),
               ':input[name="product_type"]'=> array('value' => 'c'),
            ),       
          ),
        ),    
      );
    

    What is happening is that what ever the last value in the page is being used. I have found the following pages https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7 & https://drupal.org/node/1464758 but am still lost.

  •  Answers:

  • Enxebre

    it is no right at all, this is the right way to make an OR:

    $form['problem'] = array(
      '#type' => 'textarea',
      '#title' => t('Problem'),
      '#states' => array(
        'visible' => array(
          array(
            ':input[name="product_type"]'=> array(
              array('value' => 'b'),
              array('value' => 'c'),
            ),
          ),
        ),
      ),
    );
    

    Regards

  • Mike Vranckx

    Try putting your condition like this (instead of 2 different entries):

    $form['problem'] = array(
        '#type' => 'textarea',
        '#title' => t('Problem'),
        '#states' => array(
            'visible' => array(
                ':input[name="product_type"]'=> array(
                    'value' => 'b',
                    'value' => 'c',
                ),     
            ),
        ),    
    );