You are here

public static function Registrants::validateCreate in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Element/Registrants.php \Drupal\rng\Element\Registrants::validateCreate()
  2. 3.x src/Element/Registrants.php \Drupal\rng\Element\Registrants::validateCreate()

Validate identity creation sub-form.

Parameters

array $form: The complete form.

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

File

src/Element/Registrants.php, line 785

Class

Registrants
Provides a form element for a registrant and person association.

Namespace

Drupal\rng\Element

Code

public static function validateCreate(array &$form, FormStateInterface $form_state) {
  $element = RegistrantsElement::findElement($form, $form_state);
  $utility = new RegistrantsElement($element, $form_state);
  $utility
    ->buildRegistrant(TRUE);
  $new_person_tree = array_merge($element['#parents'], [
    'entities',
    'person',
    'new_person',
    'newentityform',
  ]);
  $subform_newentity = NestedArray::getValue($form, $new_person_tree);
  $value = $form_state
    ->getTemporaryValue(array_merge([
    '_registrants_values',
  ], $element['#parents']));
  $form_state
    ->setValue($element['#parents'], $value);
  $new_person = $form_state
    ->get('newentity__entity');
  $form_display = $form_state
    ->get('newentity__form_display');
  $form_display
    ->extractFormValues($new_person, $subform_newentity, $form_state);
  $form_display
    ->validateFormValues($new_person, $subform_newentity, $form_state);

  /** @var \Symfony\Component\Validator\ConstraintViolationListInterface $violations */
  $violations = $new_person
    ->validate();
  if ($violations
    ->count() == 0) {
    $form_state
      ->set('newentity__entity', $new_person);
  }
  else {
    $triggering_element = $form_state
      ->getTriggeringElement();
    foreach ($violations as $violation) {
      $form_state
        ->setError($triggering_element, (string) $violation
        ->getMessage());
    }
  }
}