Hi,
I'm working on drupal 7 module similar to the conditional_fields module for drupal 6. This module will set #states attribute for the form field depend on the value selected from other form fields.
So far #states attribute works only for one field value that's why I made small patch for the states.js file which allows to define array of the values for the selector :
'visible' => array(
JQUERY_SELECTOR => array('value' => array('value_1', 'value_2', ....)),
...
),
I think it would be useful to add this patch to drupal 7 core.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

redndahead’s picture

Priority: Normal » Major
Status: Needs review » Needs work

The patch is missing. I think this is really important can you please post it?

redndahead’s picture

Version: 7.0-rc2 » 7.x-dev
Priority: Major » Normal
Status: Needs work » Needs review
FileSize
40.15 KB

Here is my attempt.

redndahead’s picture

hmm let's put the right patch this time

Status: Needs review » Needs work

The last submitted patch, 1001172-mulivalue_states-1.patch, failed testing.

redndahead’s picture

Status: Needs work » Needs review

don't know why tests would be failing retesting.

redndahead’s picture

Issue tags: -FAPI #states, -multivalue

#3: 1001172-mulivalue_states-1.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1001172-mulivalue_states-1.patch, failed testing.

redndahead’s picture

Status: Needs work » Needs review
Issue tags: +FAPI #states, +multivalue

#3: 1001172-mulivalue_states-1.patch queued for re-testing.

redndahead’s picture

This would be the eventual complete fix in d8. #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions But I think this is a good middle ground for d7. The syntax changes compared to the d8 patch is minimal.

redndahead’s picture

FileSize
10.95 KB

Here is a module to test with. Apply the patch and go to yoursite.com/examples/form_example/states
Text will appear if you choose "Four" or "Lots" in the drop down.

Here is the form code used.

$form['how_many_years'] = array(
    '#type' => 'select',
    '#title' => t('How many years have you completed?'),
    // The options here are integers, but since all the action here happens
    // using the DOM on the client, we will have to use strings to work with
    // them.
    '#options' => array(
      1 => t('One'),
      2 => t('Two'),
      3 => t('Three'),
      4 => t('Four'),
      5 => t('Lots'),
    ),
  );

  $form['comment'] = array(
    '#type' => 'item',
    '#description' => t("Wow, that's a long time."),
    '#states' => array(
      'visible' => array(
        // Note that '5' must be used here instead of the integer 5.
        // The information is coming from the DOM as a string.
        ':input[name="how_many_years"]' => array('value' => array('4', '5')),
      ),
    ),
  );
BenK’s picture

Subscribing

rfay’s picture

Version: 7.x-dev » 8.x-dev

While this has clear merit, it's actually a little tiny API change, and I just would rather not do that now, especially when changes like this can be done in contrib. #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions was probably the right way to do this and it didn't get in.

Sorry, but IMO this should wait, and please make a nice module and link to it here because everybody will want to do this.

AdrianB’s picture

Subscribing.

redndahead’s picture

Status: Needs review » Closed (duplicate)

I'm going to mark this as a duplicate so it doesn't linger. Not really necessary if it isn't going in d7.

duplicate of #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions

peem83’s picture

FileSize
381 bytes

This patch allows to build OR constructions like in example in the issue description :

'visible' => array(
JQUERY_SELECTOR => array('value' => array('value_1', 'value_2', ....)),
...
),

the field will be visible if selector has value_1 OR value_2

redndahead’s picture