You are here

public function CalendarEventForm::validateForm in Opigno calendar event 8

Same name and namespace in other branches
  1. 3.x src/Form/CalendarEventForm.php \Drupal\opigno_calendar_event\Form\CalendarEventForm::validateForm()

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/CalendarEventForm.php, line 71

Class

CalendarEventForm
Form handler for calendar event type forms.

Namespace

Drupal\opigno_calendar_event\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Call parent validation.
  $result = parent::validateForm($form, $form_state);

  // Custom validation.
  $values = $form_state
    ->getValues();
  if (isset($values['date_daterange'][0]['value_wrapper']['date']) && !empty($values['date_daterange'][0]['value_wrapper']['date']) && isset($values['date_daterange'][0]['end_value_wrapper']['date']) && !empty($values['date_daterange'][0]['end_value_wrapper']['date'])) {
    $pattern = \Drupal::config('core.date_format.datepicker')
      ->get('pattern');
    $date = OpignoDateRangeWidget::createDateTimeFromWrapper($values['date_daterange'][0]['value_wrapper'], $pattern);
    $end_date = OpignoDateRangeWidget::createDateTimeFromWrapper($values['date_daterange'][0]['end_value_wrapper'], $pattern);
    if ($end_date < $date) {
      $form_state
        ->setError($form['date_daterange']['widget'][0]['end_value_wrapper'], $this
        ->t('The end date cannot be before the start date'));
    }
  }
  return $result;
}