You are here

public function ModuleResultForm::validateForm in Opigno module 8

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

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/ModuleResultForm.php, line 142

Class

ModuleResultForm
Class ModuleResultForm.

Namespace

Drupal\opigno_module\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $answer_storage = \Drupal::entityTypeManager()
    ->getStorage('opigno_answer');
  $form_values = $form_state
    ->getValues();
  foreach ($form_values['answers'] as $answer_id => $value) {

    // Check if score is lower than maxScore.
    if (isset($value['score'])) {
      $answer = $answer_storage
        ->load($answer_id);
      $max_score = $this
        ->getAnswerMaxScore($answer);
      if (intval($value['score'] > intval($max_score))) {
        $form_state
          ->setErrorByName('score', $this
          ->t("Score can't be greater than maxScore."));
      }
    }
  }
}