BooleanCheckboxWidget doesn't respect #required_error

Created on 26 February 2024, about 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/BooleanCheckboxWidget.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. See issue 3060626 πŸ› OptionsWidgetBase doesn't respect #required_error Fixed

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

Active

Version

10.3 ✨

Component
FormΒ  β†’

Last updated 21 minutes ago

Created by

πŸ‡©πŸ‡ͺGermany Christian.wiedemann

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