You are here

public function ILTForm::validateForm in Opigno Instructor-led Trainings 8

Same name and namespace in other branches
  1. 3.x src/Form/ILTForm.php \Drupal\opigno_ilt\Form\ILTForm::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/ILTForm.php, line 201

Class

ILTForm
Provides a form for creating/editing a opigno_ilt entity.

Namespace

Drupal\opigno_ilt\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $date = $form_state
    ->getValue('date');
  if (isset($date[0]['value_wrapper']) && isset($date[0]['end_value_wrapper'])) {
    if (empty($date[0]['value_wrapper']['date']) || empty($date[0]['end_value_wrapper']['date'])) {
      return $form_state
        ->setError($form['date'], $this
        ->t('Date fields can not be empty'));
    }
    $start_date = OpignoDateRangeWidget::createDateTimeFromWrapper($date[0]['value_wrapper']);
    $end_date = OpignoDateRangeWidget::createDateTimeFromWrapper($date[0]['end_value_wrapper']);
    if (isset($start_date) && $end_date < $start_date) {
      $form_state
        ->setError($form['date'], $this
        ->t('The end date cannot be before the start date'));
    }
  }
}