1

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.

1

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

  • I can't get this to work, it just seems to display regardless of which options are checked. I can get it to work when checking a single option. – Daniel Harper Oct 23 '13 at 11:16
  • If you can't get this to work and you are using the jquery update module then apply the patch found over here. – Felix Eve Mar 7 '14 at 10:21
0

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',
            ),     
        ),
    ),    
);
  • This won't work because you have 2 keys that are both "value" so the second one will overwrite the first. – Felix Eve Mar 7 '14 at 9:58
0

I've got the same issue. But for me works this solution https://www.drupal.org/node/735528#comment-9813979

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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