Datetime form element cannot validate when using a format without seconds

Created on 11 May 2016, over 8 years ago
Updated 7 February 2023, almost 2 years ago

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

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

<!-- See https://drupal.org/core-mentoring/novice-tasks for tips on identifying novice tasks. Delete or add "Novice" from the Novice? column in the table below as appropriate. Uncomment tasks as the issue advances. Update the Complete? column to indicate when they are done, and maybe reference the comment number where they were done. -->

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Form 

Last updated 5 days ago

Created by

🇫🇷France duaelfr Montpellier, France

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.

Production build 0.71.5 2024