You are here

public function TimeRangePickerWidget::validateStartEnd in Time Field Picker 8.5

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

src/Plugin/Field/FieldWidget/TimeRangePickerWidget.php, line 138

Class

TimeRangePickerWidget
Plugin implementation of the 'time_range_picker_widget' widget.

Namespace

Drupal\time_picker\Plugin\Field\FieldWidget

Code

public function validateStartEnd(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $start_time = $element['start']['#value'];
  $end_time = $element['end']['#value'];
  if (!empty($start_time) && !empty($end_time)) {
    $tz = date_default_timezone_get();
    $start = new DrupalDateTime($start_time, $tz);
    $end = new DrupalDateTime($end_time, $tz);
    if ($start > $end) {
      $form_state
        ->setError($element, $this
        ->t('Please enter valid time formate : End time greater than Start time.'));
    }
  }
}