This is not the same as #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions

Much simpler: A simple OR condition can be expressed as jQuery selector like this:

        '#states' => array(
          'visible' => array(
            ':input[name="checkboxes[foo]"], :input[name="checkboxes[bar]"]' => array('checked' => TRUE),
          ),
        ),

Translating:

  1. The selector matches more than one checkbox.
  2. The element should be visible when at least one of the checkboxes is checked.
  3. When no checkbox is checked, the element should not be visible.

Attached patch makes that work.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dmitriy.trt’s picture

subscribing

Dmitriy.trt’s picture

Status: Needs review » Reviewed & tested by the community

It works, thanks a lot! Patch makes it possible to depend on "checkboxes" element with simple condition like:

'#states' => array(
  'invisible' => array(
    'input[name^="checkboxes_name"]' => array('checked' => FALSE)
  )
)

This means "element is invisible if all of checkboxes are unchecked".

rfay’s picture

Version: 7.x-dev » 8.x-dev
Status: Reviewed & tested by the community » Needs review

D8 first. Let's get these fixed on D8 and into D7.

sun’s picture

drupal.states-checked.0.patch queued for re-testing.

james.williams’s picture

subscribe

geerlingguy’s picture

Sub.

nagiek’s picture

Patch works for me, running Drupal 7.10. Handy!

nagiek’s picture

Status: Needs review » Reviewed & tested by the community
sun’s picture

FileSize
942 bytes

Re-rolled against latest HEAD.

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +Needs backport to D7

Looks fine, has had manual testing, shame we don't have qunit for stuff like this.

Committed/pushed to 8.x, back to 7.x for backport.

Albert Volkman’s picture

Status: Patch (to be ported) » Needs review
FileSize
788 bytes

D7 backport.

Albert Volkman’s picture

bump (and updated patch to apply cleanly)

sun’s picture

This patch looks safe to backport for me, but I wonder whether the jQuery in D7 core supports .prop()?

(I guess it does, since I originally rolled this patch for D7, but I'm not 100% sure.)

Albert Volkman’s picture

Good point. Just checked this and D7 has jQuery 1.4.4-

http://drupalcode.org/project/drupal.git/blob/refs/heads/7.x:/misc/jquer...

and .prop() wasn't introduced until jQuery 1.6-

http://api.jquery.com/prop/

Perhaps we should use .is() instead?

sun’s picture

Assigned: sun » nod_

Perhaps @nod_ can help out with those question.

Dmitriy.trt’s picture

FileSize
1.14 KB

Patch #14 doesn't work because of missing colon before "checked" pseudo-class. Updated patch attached.

jason.fisher’s picture

@16

Are you missing the check to see if it is checked? It seems like you are always returning the state of the first checkbox?

Dmitriy.trt’s picture

@jason.fisher,

Innermost function breaks the jQuery.each() loop only if checked checkbox is found. Otherwise the loop will move to the next one. See jQuery API documentation:

You can stop the loop from within the callback function by returning false.

mgifford’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll

Not surprising that the patch from 2 years ago needs a reroll.

rpayanm’s picture

Status: Needs work » Needs review
FileSize
711 bytes
mgifford’s picture

Issue tags: -Needs reroll
mgifford’s picture

Assigned: nod_ » Unassigned

@rpayanm reroll applies nicely in SimplyTest.me

What do we need to do to test this patch to mark it RTBC?

  • catch committed 56a7a11 on 8.3.x
    Issue #1057748 by sun: Fixed #states selector matching multiple...

  • catch committed 56a7a11 on 8.3.x
    Issue #1057748 by sun: Fixed #states selector matching multiple...

  • catch committed 56a7a11 on 8.4.x
    Issue #1057748 by sun: Fixed #states selector matching multiple...

  • catch committed 56a7a11 on 8.4.x
    Issue #1057748 by sun: Fixed #states selector matching multiple...

  • catch committed 56a7a11 on 9.1.x
    Issue #1057748 by sun: Fixed #states selector matching multiple...