Problem/Motivation
When using a datetime form element that does not allow to input seconds as follow:
$form['datetime_without_seconds'] = [
'#type' => 'datetime',
'#title' => $this->t('Datetime without seconds'),
'#default_value' => new DrupalDateTime(),
'#date_time_element' => 'text',
'#date_time_format' => 'H:i',
];
And submitting the form without changing the value, we get a validation error.
It's caused by the \Drupal\Core\Datetime\Element\Datetime::valueCallback()
method that adds ":00" to times that don't have seconds. The associated comment says "Seconds will be omitted in a post in case there's no entry." so I can't understand why it's been done.
Proposed resolution
Only add that seconds when the time format needs it.
Fix the logic in Datetime:valueCallback() for massaging input values so that DrupalDateTime::createFromFormat() does the actual work based on the format specified by the form element.
Remaining tasks
Task | Novice task? | Contributor instructions | Complete? |
---|---|---|---|
Create a patch | Instructions | Done | |
Update the issue summary | Instructions | Done | |
Add automated tests | Instructions | Done | |
Manually test the patch | Novice | Instructions | |
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards | Instructions |
User interface changes
None.
API changes
None.
Data model changes
None.