Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DateRangeWidgetBase.php

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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal