#states not working correctly when built from a logical combination of multliple fields

Created on 8 September 2023, about 1 year ago

Problem/Motivation

I noticed this issue while developing a relatively complex form, but I have since refined the problem down to a very simple form and have tested on a vanilla instance of Drupal 9.5.10.

The issue occurs when an element's state is built from a logical combination of multiple other fields.

For example:

  'visible' => [
    ':input[name="select1"]' => ['value' => 0],
    ':input[name="select2"]' => ['value' => 1],
  ],

Also note the same behaviour occur if written:

  'visible' => [
    ':input[name="select1"]' => ['value' => 0],
    'and',
    ':input[name="select2"]' => ['value' => 1],
  ],

Steps to reproduce

If using the following form:

  $form['select1'] = [
    '#type' => 'select',
    '#title' => 'Select 1',
    '#options' => [0 => 0, 1 => 1],
    '#default_value' => 0,
  ];

  $form['select2'] = [
    '#type' => 'select',
    '#title' => 'Select 2',
    '#options' => [0 => 0, 1 => 1],
    '#default_value' => 0,
  ];

  $form['select3'] = [
    '#type' => 'select',
    '#title' => 'Select 3',
    '#options' => [0 => 0, 1 => 1],
    '#states' => [
      'visible' => [
        ':input[name="select1"]' => ['value' => 0],
        'and',
        ':input[name="select2"]' => ['value' => 1],
      ],
    ],
  ];

After the form loads, you see the select elements 1 and 2. You don't see select 3 because the default values aren't as defined in #states.

If I select a value of 1 in select element 2, the values *should* be correct to trigger the appearance of select element 3. However, this doesn't happen.

Instead you have to set element 2 back to a value of 0, then again to a value of 1, before select element 3 appears.

๐Ÿ› Bug report
Status

Active

Version

9.5

Component
Formย  โ†’

Last updated 2 days ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom Ashley George

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024