Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292228

Datetime form element cannot validate when using a format without seconds

$
0
0

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

Contributor tasks needed
TaskNovice task?Contributor instructionsComplete?
Create a patchInstructionsDone
Update the issue summaryInstructionsDone
Add automated testsInstructionsDone
Manually test the patch NoviceInstructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standardsInstructions

User interface changes

None.

API changes

None.

Data model changes

None.


Viewing all articles
Browse latest Browse all 292228

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>