You are here

public function DatetimeRangeAllDayWidget::validateStartEnd in Date all day 8

#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.

Overrides DateRangeWidgetBase::validateStartEnd

File

src/Plugin/Field/FieldWidget/DatetimeRangeAllDayWidget.php, line 61

Class

DatetimeRangeAllDayWidget
Plugin implementation of the 'daterange_all_day' widget.

Namespace

Drupal\date_all_day\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) {
    if (!$this
      ->getFieldSetting('optional_end_date') && $end_date === NULL) {
      $form_state
        ->setError($element['end_value'], $this
        ->t('The @title end date is required', [
        '@title' => $element['#title'],
      ]));
    }
    if ($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'],
          ]));
        }
      }
    }
  }
}