function DateRangeWidgetBase::validateStartEnd

Same name in other branches
  1. 9 core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase::validateStartEnd()
  2. 8.9.x core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase::validateStartEnd()
  3. 11.x 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'],
                ]));
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.