5

Using form API in D7, I need to use from states to setup a visible input with this condition :

field1 = value A AND (field2 = value B OR value C OR value D)

Something like this, but this example doesn't run :

'#states' => array(
      'visible' => array(
        array(':input[name="question_inhabitant_type"]' => array('value' => 'owner')),
        'AND',
        array(':input[name="question_building_type"]' => array('value' => 'house100')),
        'OR',
        array(':input[name="question_building_type"]' => array('value' => 'house150')),
        'OR',
        array(':input[name="question_building_type"]' => array('value' => 'house200')),
      ),
    ),

Any idea?

1 Answer 1

6

You can use or/xor in form states. For and, continuation of the conditions is enough. For example your code the condition can be written as:

'#states' => array(
  'visible' => array(
    array(':input[name="question_inhabitant_type"]' => array('value' => 'owner')),
    array(
      array(':input[name="question_building_type"]' => array('value' => 'house100')),
      'or',
      array(':input[name="question_building_type"]' => array('value' => 'house150')),
      'or',
      array(':input[name="question_building_type"]' => array('value' => 'house200')),
    ),
  ),
),

Note: jQuery 1.7 doesn't support OR/XOR's for #states. So, make sure you have latest jQuery version on you site (>1.7).

Unfortunately the above code didn't worked as expected. Please help through your comments to correct it. Also meanwhile you can try this code:

'#states' => array(
  'visible' => array(
    ':input[name="question_inhabitant_type"]' => array('value' => 'owner'),
    ':input[name="question_building_type"]' => array(
      array('value' => 'house100'),
      array('value' => 'house150'),
      array('value' => 'house200'),
    ),
  ),
),
3
  • Jquery default version is set on 1.10. According to your example if I set only "or" condition it's working. If I copy paste the code, it's doesn't run :'(
    – Promo
    Feb 24, 2016 at 13:07
  • can you please try the code provided on bottom? Feb 24, 2016 at 14:36
  • the second example is running :D ty
    – Promo
    Feb 24, 2016 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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