Problem/Motivation

A datetime field when configured with the Date and Time widget, is missing the field's name in the validation error message when it fails validation. The problem is caused by the way the widget is rendered; it omits setting the $element['#title'] as the latter is rendered as part of a parent fieldset instead. Whilst there is nothing wrong with this, the Datetime validation logic uses the $element['#title']'s value to determine the field name.

Steps to reproduce

  1. Add a datetime field to a content type
  2. Set the datetime field as required
  3. Set the datetime field's form display to use the Date and Time field widget in Form display
  4. Create a new node of the content type and save the form without filling the datetime field
  5. Observe that the validation error message produced is missing the datetime field name

Proposed resolution

Align the $title parsing logic in the Datetime.php class with the one from the Datelist.php class which has fallback logic on the element's parent.

Remaining tasks

n/a

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

Issue fork drupal-3354606

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pascalim created an issue. See original summary.

pascalim’s picture

Status: Active » Needs review
abhijith s’s picture

StatusFileSize
new387.98 KB
new442.62 KB

Applied MR !3838. The fieldname is displaying in the error message after applying this patch.

Attaching screenshots below.

Before patch:

After patch:

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Next step would be to add tests showing the issue.

ranjith_kumar_k_u’s picture

Version: 9.5.x-dev » 10.1.x-dev
StatusFileSize
new1.38 KB
new1.23 KB
new2.26 KB

Added tests.

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Think the test should check the entire message. The fact the message changed and didn't break anything probably showed it wasn't being tested to begin with.

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
StatusFileSize
new2.7 KB
new1.69 KB

The date format value in the error message is a dynamic one(The Field_label date is required. Please enter a date in the format 2023-05-10 05:19:38.), and the date format value is created on the basis of when the validateDatetime function is calling.

So I have added one more assertion to verify date format value is not empty and is in the following format "YYYY-MM-DD HH:MM:SS".
$this->assertMatchesRegularExpression('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $arguments['%format']);

borisson_’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

#9 looks like a good fix for the missing test coverage.

  • justafish committed 1aaa480b on 11.x
    Issue #3354606 by ranjith_kumar_k_u, pascalim, Abhijith S, smustgrave,...
justafish’s picture

Version: 10.1.x-dev » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 1aaa480b07 and pushed to 11.x. Thanks!

  • justafish committed 8d221fe1 on 10.1.x
    Issue #3354606 by ranjith_kumar_k_u, pascalim, Abhijith S, smustgrave,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.