OptionsWidgetBase doesn't respect #required_error

Created on 10 June 2019, over 5 years ago
Updated 26 May 2023, over 1 year ago

Problem/Motivation

When setting #required_error on a select form widget using the form alter API, core doesn't implement custom error message.
Problem is that core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php doesn't consider the #required_errorform array attribute set by the form alter.

Steps to reproduce

Following example helps to reproduce the issue:
Add a new required field to User Account name "type" of type List (text)
Alter the form and set the custom required error message into #required_error:

<?php
function my_module_form_user_form_alter(&$form, FormStateInterface $form_state) {
   $form['field_type']['widget']['#required_error'] = t('CUSTOM REQUIRED ERROR MESSAGE');
}
?>

Whenever above form alter is executed, custom error message set in #required_error is getting ignored by OptionsWidgetBase.php's validateElement method.

Proposed resolution

First check if any value is being set in #required_error attribute.
If value is not set, then display the default error message.

Remaining tasks

Needs review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

🐛 Bug report
Status

Fixed

Version

10.0

Component
Base 

Last updated about 1 hour ago

Created by

🇷🇴Romania aalin

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024