public function SingleDateTimeRangeWidget::validateSingleDateTime in Single DateTimePicker 8
Callback #element_validate 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
- modules/
single_datetime_range/ src/ Plugin/ Field/ FieldWidget/ SingleDateTimeRangeWidget.php, line 121
Class
- SingleDateTimeRangeWidget
- Plugin implementation of the 'daterange_default' widget.
Namespace
Drupal\single_datetime_range\Plugin\Field\FieldWidgetCode
public function validateSingleDateTime(array &$element, FormStateInterface $form_state, array &$complete_form) {
// String to DrupalDateTime.
$start_date = new DrupalDateTime($element['value']['#value']);
$end_date = new DrupalDateTime($element['end_value']['#value']);
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'],
]));
}
}
}
}