Time Field fails required field validation if value is 12:00 AM

Created on 22 November 2018, about 6 years ago
Updated 2 August 2023, over 1 year ago

There is a problem with the time field storing the value of midnight as the integer 0, and the isEmpty() methods on the classes in the Drupal\time_field\Plugin\Field\FieldType namespace.

Time field currently represents its values as integer values, and that causes an issue with 12:00:00AM being stored as (int) 0, and returned from \Drupal\time_field\Element\TimeElement::valueCallback() as the same. This causes issues with \Drupal\time_field\Plugin\Field\FieldType\TimeType::isEmpty() performing an empty() check on a value of (int) 0

Additionally, in \Drupal\Core\Form\FormValidator::doValidateForm(), we can see from the comments that treatment of integer values of 0 is different from string values ('0'). Core is using integer 0 values to represent empty values, and that is definitely not the case here. There is a line (for me, 262) in this method that encapsulates this:

$is_empty_value = ($elements['#value'] === 0);

You can confirm this is an issue by adding a required test time field to any content type. If you set the value to 12:00 AM, it fails validation, and no value is saved.

To fix, \Drupal\time_field\Element\TimeElement::valueCallback() should return its timestamps as string values to differentiate from empty values, and theDrupal\time_field\Plugin\Field\FieldType classes should change their isEmpty() methods to only use NULL as a valid empty value.

Changes:

  • Update \Drupal\time_field\Element\TimeElement::valueCallback() to return a string.
  • Update \Drupal\time_field\Plugin\Field\FieldType\TimeRangeType::isEmpty() to test against NULL values instead of using empty()
  • Update \Drupal\time_field\Plugin\Field\FieldType\TimeType::isEmpty to test against NULL values instead of using empty()

Patches forthcoming.

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States aron.beal North Vancouver, BC

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.

  • πŸ‡¨πŸ‡¦Canada mdolnik

    The original merged in fix for this issue is not sufficient as it will still cause a validation error when the time is set to midnight as the value resolves to 0 which is treated as empty by Drupal core.

    The original patch supplied in comment #5 had logic in TimeElement::valueCallback() which cast the return value as a string which is missing from both the current 1.x and 2.x branches.

    This string cast is necessary to avoid FormValidator::doValidateForm() from considering midnight to be empty.
    ie: the line with $is_empty_value = ($elements['#value'] === 0); will treat 0 (midnight) as an empty value where-as it will treat '0' as populated.

    Regardless I believe the updated patch in #9 should work for anyone needing this fix in 1.x and for 2.x there is an open issue 3227128 πŸ› Time Field fails required field validation if value is 00:00 Needs work which I think should be used to resolve this in version 2.x.

Production build 0.71.5 2024