Problem/Motivation
I noticed a strange bug in a custom form using the datetime
form element type.
My desire was to collect only a date, without a time. I want to use datetime
instead of date
because it allows a DrupalDateTime
object to be used as the #default_value
, which is then also available in validation/submit methods. The docs state that '#date_time_element' => 'none',
can be used to "Do not display a time element."
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%...
However, the resulting DrupalDateTime
object in form validation/submit does NOT have a time of "midnight" as I would expect. Instead it is mysteriously populated with the current time (regardless of what the date is set to).
Steps to reproduce
The following code snippet demonstrates a simple form element where this issue occurs:
$form['datetime'] = [
'#type' => 'datetime',
'#title' => 'Datetime',
'#default_value' => new DrupalDateTime(date('Y-m-d')),
'#date_time_element' => 'none',
];
- Loading the form shows the date element, with the current date filled in as the default value.
- The
new DrupalDateTime(date('Y-m-d'))
creates a date object that does NOT include a time, as is expected. (In other words, it is midnight of the current date.)
- Upon form submission, I would expect that
$form_state->getValue('datetime')
would give me a similar date object - with a time of midnight.
- In reality, the date object that is created ends up with the *current time*, even though that time was no where in the form inputs or default value.
For example, using the above code in an example form right now, it shows an HTML5 date field with a value of "02/03/2023". When I submit the form, however, and run the following code in validate/submit, the result is not what I would expect:
date('c', $form_state->getValue('datetime')->getTimestamp()
Output:
2023-02-03T16:33:06+00:00
If I comment out the '#date_time_element' => 'none',
line, however, it works as expected. When the form is built I see the date element with a value of "02/03/2023" and a time element with a value of "12:00:00 AM". And when I submit the form, the result is what I would expect:
date('c', $form_state->getValue('datetime')->getTimestamp()
Output:
2023-02-03T00:00:00+00:00
Proposed resolution
TBD
Remaining tasks
TBD
User interface changes
TBD
API changes
TBD
Data model changes
TBD
Release notes snippet
TBD