PrimitiveTypeConstraintValidator error when using #element_validate on a Date Range field

Created on 8 September 2017, over 7 years ago
Updated 20 July 2023, over 1 year ago

When trying to implements an #element_validate callback on the start date element of a Date Range field :

$form['field_candidates_unavailability']['widget'][0]['value']['#element_validate'][] = [$this, 'validateUnavailabiltyDates'];

I get a "This value should be of the correct primitive type." error on validation. The value has not been modified, just a simple diff test with the today date :

$start_value = $form_state->getValue('field_candidates_unavailability');
/** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
$start_date = $start_value[0]['value']['object'];
$today = new DrupalDateTime('now', drupal_get_user_timezone());
datetime_date_default_time($today);
$diff = $today->diff($start_date);
if ($diff->days == 1 && $diff->invert == 1) {
  // Start date is anterior to today, throw an error.
  $form_state->setErrorByName(implode('][', $element['#parents']), 'The start date must be posterior to today.');
}

I can see that PrimitiveTypeConstraintValidator::validate method get an array for the field $value, instead of a date in string format.

I've to do this weird trick in the #element_validate callback to make this PrimitiveTypeConstraintValidator works :

$form_state->setValue($element['#parents'], $start_value[0]['value']['date']);
πŸ› Bug report
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
DatetimeΒ  β†’

Last updated 6 days ago

Created by

πŸ‡«πŸ‡·France mattew

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Since there hasn't been a follow up to #13 going to close for now.

    If still an issue please reopen updating the issue summary as needed.

    Thanks!

Production build 0.71.5 2024