BooleanCheckboxWidget doesn't respect #required_error

Created on 26 February 2024, 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/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 10 days 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

  • Issue created by @Christian.wiedemann
  • Pipeline finished with Failed
    over 1 year ago
    Total: 221s
    #104116
  • Pipeline finished with Failed
    over 1 year ago
    #104140
  • Pipeline finished with Success
    over 1 year ago
    Total: 486s
    #104143
  • 🇳🇿New Zealand quietone

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

  • 🇳🇿New Zealand quietone

    Really change to 11.x

  • Pipeline finished with Success
    2 months ago
    Total: 535s
    #547854
  • Pipeline finished with Failed
    2 months ago
    Total: 483s
    #547871
  • Merge request !12724Add validate method → (Open) created by Christian.wiedemann
  • 🇩🇪Germany Christian.wiedemann

    christian.wiedemann changed the visibility of the branch 3423819-booleancheckboxwidget-doesnt-respect to hidden.

  • Status changed to Needs review 2 months ago
  • Pipeline finished with Failed
    2 months ago
    Total: 538s
    #547878
  • 🇺🇸United States smustgrave

    Can we get a test case showing the problem.

  • 🇮🇳India abhijith s

    Getting this error after applying latest MR. Seems like validateElement method needs to be added in Widget.

    The website encountered an unexpected error. Try again later.
    
    TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, class Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget does not have a method "validateElement" in call_user_func_array() (line 283 of core/lib/Drupal/Core/Form/FormValidator.php).
    Drupal\Core\Form\FormValidator->doValidateForm() (Line: 239)
    Drupal\Core\Form\FormValidator->doValidateForm() (Line: 239)
    Drupal\Core\Form\FormValidator->doValidateForm() (Line: 239)
    Drupal\Core\Form\FormValidator->doValidateForm() (Line: 118)
    Drupal\Core\Form\FormValidator->validateForm() (Line: 585)
    Drupal\Core\Form\FormBuilder->processForm() (Line: 321)
    Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult()
    call_user_func_array() (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 622)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 53)
    Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 715)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    
  • 🇮🇳India abhijith s

    Updated issue description as it appears to be copied from other issue

Production build 0.71.5 2024