#states not affecting requirement of select

Created on 29 April 2025, 2 months ago

Problem/Motivation

Cannot affect the visibility or requirement of managed_file with states. Identical to #1118016: conditional visibility of a managed_file using #states attribute does not work so either the bug wasn't fully fixed before, or a regression happened at some point and broke this functionality.

With this definition:

$form['type'] = [
  '#type' => 'select',
  '#options' => [
    'node' => t('Content'),
    'webform' => t('Webform'),
  ],
];

$form['webform'] = [
  '#type' => 'select',
  '#title' => t('Webform reference'),
  '#states' => [
    'visible'  => [':input[name="type"]' => ['value' => 'webform']],
    'required' => [':input[name="type"]' => ['value' => 'webform']],
  ],
];

I expect the "webform" element (which is based on an entity reference field to select a webform) to be visibly marked as required when "Webform" is chosen in the "type" form element. The visibility state works as expected, the required state not. Even the workaround from Conditional Form Fields – The State 'required' doesn't work:

$form['webform']['widget'][0]['target_id']['#states'] = [
  'required' => [':input[name="type"]' => ['value' => 'webform']],
];

Steps to reproduce

1. Create a content type with a "String list" field and an entity reference field for the target type "webform" (other target types might work as well) producing an edit form with structure as above
2. Create a new entity of that content type.
Problems:
1. Mandatory asterisk is not shown for webform entity reference selection field when the Webform option is selected

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

forms system

Created by

🇦🇹Austria mvonfrie

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

Comments & Activities

Production build 0.71.5 2024