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
- Add a datetime field to a content type
- Set the datetime field as
required - Set the datetime field's form display to use the
Date and Timefield widget in Form display - Create a new node of the content type and save the form without filling the datetime field
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | interdiff_6-9.txt | 1.69 KB | ranjith_kumar_k_u |
| #9 | 3354606-9.patch | 2.7 KB | ranjith_kumar_k_u |
| #6 | 3354606-6.patch | 2.26 KB | ranjith_kumar_k_u |
| #6 | interdiff_2-6.txt | 1.23 KB | ranjith_kumar_k_u |
| #6 | 3354606-6-test-only.patch | 1.38 KB | ranjith_kumar_k_u |
Issue fork drupal-3354606
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:
- 3354606-datetime-field-name
changes, plain diff MR !3838
Comments
Comment #3
pascalim commentedComment #4
abhijith s commentedApplied MR !3838. The fieldname is displaying in the error message after applying this patch.
Attaching screenshots below.
Before patch:

After patch:

Comment #5
smustgrave commentedNext step would be to add tests showing the issue.
Comment #6
ranjith_kumar_k_u commentedAdded tests.
Comment #7
ranjith_kumar_k_u commentedComment #8
smustgrave commentedThink 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.
Comment #9
ranjith_kumar_k_u commentedThe 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']);Comment #10
borisson_#9 looks like a good fix for the missing test coverage.
Comment #12
justafishCommitted 1aaa480b07 and pushed to 11.x. Thanks!