0

I have looked at previous posts but can't seem to get the following to work. It just displays the form line all the time, I can get it to work when checking for just a single option.

$form['type'] = array(
  '#type' => 'radios',
  '#options' => array(
    '10' => '10',
    '29' => '29',
    '30' => '30',
  ),
  '#title' => '',
);

$form['line'] = array(
  '#type' => 'radios',
  '#options' => array(
    '1' => '1',
    '2' => '2',
    '3' => '3',
  ),
  '#title' => '',
  '#states' => array(
    'visible' => array(
      ':input[name="type"]' => array(
        array('value' => '29'),
        array('value' => '30'),
      )
    ),
  ),
);
  • I think I have confused myself a little, do you mean you want line to appear when both type 29 AND type 30 are checked? That's what it sounds like but that is impossible with radios because you can only check one at a time. - Or should your question say "I can get it to work when checking for just a single option." – rooby Oct 23 '13 at 13:13
  • I want the OR functionality but it isn't wotking. I'm actually getting a javascript error message Uncaught TypeError: Object 0 has no method 'charAt' just looking into it now – Daniel Harper Oct 23 '13 at 13:29
  • That code you have actually does what you want. The javascript error you have is unrelated to the states code but is breaking other js on the page, including the states code. – rooby Oct 23 '13 at 13:30
  • Are you using jquery_update? - See drupal.org/node/1815896 - Upgrading to the latest dev version of jquery_update might help. – rooby Oct 23 '13 at 13:30
  • 2
    My advice is to never use jquery_update on drupal 7 unless you really have to, and even then do everything you can to find an alternative to achieve what you want. IMO it has gotten a lot better but it is still buggy enough I would not put it on any site I make. I have use the jqmulti module though in some cases and it has worked out well. jQuery update on drupal 6 is pretty tried and tested though. – rooby Oct 23 '13 at 13:41
3

The problem is that this will work as OR, not as AND, so one or the other will make it pass:

'#states' => array(
  'visible' => array(
    ':input[name="type"]' => array(
      array('value' => '29'),
      array('value' => '30'),
    ),
  ),
),

This will do AND, however because you cannot have 2 array keys the same, you run into trouble:

'#states' => array(
  'visible' => array(
    ':input[name="type1"]' => array(
      array('value' => '29'),
    ),
    ':input[name="type2"]' => array(
      array('value' => '30'),
    ),
  ),
),

Since the array keys you see there are just jQuery selectors the key is to use 2 different selectors that apply to the same field. For example, something like this might work:

'#states' => array(
  'visible' => array(
    ':input[name="type"]' => array(
      array('value' => '29'),
    ),
    '.form-item :input[name="type"]' => array(
      array('value' => '30'),
    ),
  ),
),

This assumes your type field is in a .form-item div. There would be a number of possibilities here depending on your markup.

However, making something show when 2 different radio buttons are checked at the same time is impossible because you can only check one at a time.

If you were using checkboxes you could make it so that line only showed if type was 29 AND 30 by doing this:

$form['type'] = array(
  '#type' => 'checkboxes',
  '#options' => array(
    '10' => '10',
    '29' => '29',
    '30' => '30',
  ),
  '#title' => '',
);

$form['line'] = array(
  '#type' => 'radios',
  '#options' => array(
    '1' => '1',
    '2' => '2',
    '3' => '3',
  ),
  '#title' => '',
  '#states' => array(
    'visible' => array(
      ':input[name="type[29]"]' => array(
        array('checked' => TRUE),
      ),
      ':input[name="type[30]"]' => array(
        array('checked' => TRUE),
      ),
    ),
  ),
);
  • I've gotten a bit confused about what the actual question is so I'm not sure about all this, but I can tweak it when I know your exact problem. – rooby Oct 23 '13 at 13:20
  • In that case, the code you already have works for me. – rooby Oct 23 '13 at 13:27

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.