You are here

public function OpignoModuleForm::validateForm in Opigno module 8

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

Class

OpignoModuleForm
Form controller for Module edit forms.

Namespace

Drupal\opigno_module\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $values = $form_state
    ->getValues();
  if (!empty($values['availability']) && $values['availability']) {

    // Validate module availability dates restricted to training.
    $open_date_empty = !is_object($values['open_date_training_module']) || empty($values['open_date_training_module']) || !$values['open_date_training_module']
      ->getTimestamp();
    $close_date_empty = !is_object($values['close_date_training_module']) || empty($values['close_date_training_module']) || !$values['close_date_training_module']
      ->getTimestamp();
    if ($open_date_empty) {
      $this
        ->messenger()
        ->addError($this
        ->t('Open date of module availability should be set.'));
      $form_state
        ->setError($form['open_date'], $this
        ->t('The %field field is invalid. Please enter a date.', [
        '%field' => $form['open_date']['widget']['#title'],
      ]));
    }
    if ($close_date_empty) {
      $this
        ->messenger()
        ->addError($this
        ->t('Open date of module availability should be set.'));
      $form_state
        ->setError($form['close_date'], $this
        ->t('The %field field is invalid. Please enter a date.', [
        '%field' => $form['close_date']['widget']['#title'],
      ]));
    }
  }
  if (isset($values['skills_active']) && $values['skills_active']['value'] == TRUE && empty($values['skill_target'])) {
    $form_state
      ->setErrorByName('skill_target', $this
      ->t('Target skill could not be empty.'));
  }
}