You are here

public function DatetimeRangeWidget::validateStartEnd in Typed Data API enhancements 8

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

src/Plugin/TypedDataFormWidget/DatetimeRangeWidget.php, line 134

Class

DatetimeRangeWidget
Plugin implementation of the 'datetime_range' widget.

Namespace

Drupal\typed_data\Plugin\TypedDataFormWidget

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'],
        ]));
      }
    }
  }
}