Problem/Motivation

Cannot affect the requirement of select widget with states. Similar to #2847425: #states not affecting visibility/requirement of managed_file.

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

Comments

mvonfrie created an issue. See original summary.

mvonfrie’s picture

mvonfrie’s picture

I have the issue with Drupal 10.4.5 (but set the version to 11.x-dev because it first must be solved there and then backported to 10.x).

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.