You are here

public function RegistrationForm::validateForm in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::validateForm()
  2. 3.x src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::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/RegistrationForm.php, line 146

Class

RegistrationForm
Form controller for registrations.

Namespace

Drupal\rng\Form

Code

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

  /** @var \Drupal\rng\RegistrationInterface $registration */
  $registration = parent::validateForm($form, $form_state);

  /** @var \Drupal\rng\RegistrantInterface[] $registrants_before */
  $registrants_before = $form_state
    ->getValue('registrants_before');

  /** @var \Drupal\rng\RegistrantInterface[] $registrants_after */
  $registrants_after = $form_state
    ->getValue([
    'people',
    'registrants',
  ]);

  // Registrants.
  $registrants_after_ids = [];
  foreach ($registrants_after as $registrant) {
    if (!$registrant
      ->isNew()) {
      $registrants_after_ids[] = $registrant
        ->id();
    }
  }

  // Delete old registrants if they are not needed.
  $registrants_delete = [];
  foreach ($registrants_before as $i => $registrant) {
    if (!$registrant
      ->isNew()) {
      if (!in_array($registrant
        ->id(), $registrants_after_ids)) {
        $registrants_delete[] = $registrant;
      }
    }
  }
  $form_state
    ->set('registrants_after', $registrants_after);
  $form_state
    ->set('registrants_delete', $registrants_delete);
  return $registration;
}