You are here

public function DateRangeWidgetBase::validateStartEnd in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase::validateStartEnd()
  2. 9 core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase::validateStartEnd()

#element_validate callback to ensure that the start date <= the end date.

Parameters

array $element: An associative array containing the properties and children of the generic form element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

File

core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php, line 115

Class

DateRangeWidgetBase
Base class for the 'daterange_*' widgets.

Namespace

Drupal\datetime_range\Plugin\Field\FieldWidget

Code

public function validateStartEnd(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $start_date = $element['value']['#value']['object'];
  $end_date = $element['end_value']['#value']['object'];
  if ($start_date instanceof DrupalDateTime && $end_date instanceof DrupalDateTime) {
    if ($start_date
      ->getTimestamp() !== $end_date
      ->getTimestamp()) {
      $interval = $start_date
        ->diff($end_date);
      if ($interval->invert === 1) {
        $form_state
          ->setError($element, $this
          ->t('The @title end date cannot be before the start date', [
          '@title' => $element['#title'],
        ]));
      }
    }
  }
}