Problem/Motivation
The States API won't pick up the value of a select field with #multiple = TRUE
option.
Steps to reproduce:
- Create two fields in a form with the following code:
$form['dependee'] = array(
'#type' => 'select',
'#options' => array(
'a' => 'Option A',
'b' => 'Option B',
'c' => 'Option C',
),
'#multiple' => TRUE,
);
$form['dependent'] = array(
'#type' => 'textfield',
'#states' => array(
'visible' => array(
'select[name="dependee[]"]' => array('value' => array('a')),
),
),
);
The dependent field will stay hidden regardless of the value of the dependee. This happens because the value of a multiple select field is an array, and States tries to compare it with the reference with a === operator, which returns always false.
Proposed resolution
Add a handler for arrays in states.Dependent.comparisons. This works with ANDed values:
'select[name="dependee[]"]' => array('value' => array('a', 'b')),
and with ORs as well (following the syntax proposed in #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions):
'select[name="dependee[]"]' => array(array('value' => array('a')), array('value' => array('c')))
Remaining tasks
- Land #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, unchecked so we have somewhere to put tests for this.
- Expand that test to cover this case.
- Upload test-only vs. full patch to verify test and fix.
- Further reviews/refinements.
- RTBC.
- Commit to D9/D8.
- Open follow-up issue to backport to D7.
User interface changes
This feature finally works as intended:
Before
![]()
After
![]()
API changes
N/A
Data model changes
N/A
Release notes snippet
TBD.