Datelist doesn't respect #required_error

Created on 8 October 2018, about 6 years ago
Updated 6 April 2023, over 1 year ago

When setting #required_error on a datelist item using the form API, core doesn't implement said custom error message. Here's an example of my form item:

$form['dob'] = [
'#title' => t('Date of birth'),
'#required' => TRUE,
'#type' => 'datelist',
'#required_error' => t('CUSTOM REQUIRED ERROR MESSAGE'),
'#date_part_order' => [
'day',
'month',
'year',
],
'#date_text_parts' => ['day', 'month', 'year'],
'#description' => '',
'#default_value' => NULL,
];

There doesn't appear to be a check in core for #required_error when setting the error message.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Base 

Last updated about 13 hours ago

Created by

🇬🇧United Kingdom jameshole21

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.

  • 🇮🇳India nikhil_110

    Attached patch against Drupal 10.1.x

  • 🇺🇸United States smustgrave

    Please include an interdiff and how the previous patch didn’t apply to receive credit for rerolling

  • Status changed to Needs review over 1 year ago
  • 🇳🇿New Zealand danielveza Brisbane, AU

    This has been blocked on tests for awhile, so added a test based on the patch in #15.

    Ideally we wouldn't introduce a new test class for this and just include it in DatelistElementFormTest, but I wasn't sure about the best approach for this. Happy for suggestions

  • Status changed to Needs work over 1 year ago
  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    Some small phpcs things still need to be fixed in the last patch. The test looks good at first glance, personally I'm not that worried about adding new test classes in kernel tests.

  • 🇮🇳India Akram Khan Cuttack, Odisha
  • 🇮🇳India Akram Khan Cuttack, Odisha

    sorry for the wrong patch #22

  • Status changed to Needs review over 1 year ago
  • 🇳🇿New Zealand danielveza Brisbane, AU

    Looks like a random fail. Seeing back to NR and requing tests

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Are the changes to

    testDatelistElement changes needed? It passes with or without the fix.

  • Status changed to Needs review over 1 year ago
  • 🇳🇿New Zealand danielveza Brisbane, AU

    Those changes are there to assert that you still get the correct error when you have not included #required_error.

    It would be passing without the patch because that was the only error that could be displayed. Now that we support #required_error we need to assert the existing error still displays when you don't have #required_error on the element.

  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States smustgrave

    Ah thanks for that @DanielVeza don't mind marking then

  • Status changed to Needs work over 1 year ago
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
    +++ b/core/tests/Drupal/KernelTests/Core/Datetime/DatelistElementRequiredError.php
    @@ -0,0 +1,87 @@
    +class DatelistElementRequiredError extends KernelTestBase implements FormInterface {
    

    Can we reuse the existing test here? we could use a data provider to pass required error and expected error.

    Would also be good to get a red/green set of patches showing the test catches the bug.

Production build 0.71.5 2024