You are here

public static function SmartDateWidgetBase::validateStartEnd in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  2. 8 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  3. 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  4. 3.1.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  5. 3.2.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  6. 3.3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()
  7. 3.4.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::validateStartEnd()

Ensure that the start date <= the end date via #element_validate callback.

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/Field/FieldWidget/SmartDateWidgetBase.php, line 360

Class

SmartDateWidgetBase
Base class for the 'smartdate_*' widgets.

Namespace

Drupal\smart_date\Plugin\Field\FieldWidget

Code

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