You are here

public function NestedEntityTestForm::validateForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/modules/field_test/src/Form/NestedEntityTestForm.php \Drupal\field_test\Form\NestedEntityTestForm::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

core/modules/field/tests/modules/field_test/src/Form/NestedEntityTestForm.php, line 71

Class

NestedEntityTestForm
Provides a form for field_test routes.

Namespace

Drupal\field_test\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $entity_1 = $form_state
    ->get('entity_1');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_1 */
  $form_display_1 = $form_state
    ->get('form_display_1');
  $form_display_1
    ->extractFormValues($entity_1, $form, $form_state);
  $form_display_1
    ->validateFormValues($entity_1, $form, $form_state);
  $entity_2 = $form_state
    ->get('entity_2');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_2 */
  $form_display_2 = $form_state
    ->get('form_display_2');
  $extracted = $form_display_2
    ->extractFormValues($entity_2, $form['entity_2'], $form_state);

  // Extract the values of fields that are not rendered through widgets, by
  // simply copying from top-level form values. This leaves the fields that
  // are not being edited within this form untouched.
  // @see \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
  foreach ($form_state
    ->getValues()['entity_2'] as $name => $values) {
    if ($entity_2
      ->hasField($name) && !isset($extracted[$name])) {
      $entity_2
        ->set($name, $values);
    }
  }
  $form_display_2
    ->validateFormValues($entity_2, $form['entity_2'], $form_state);
}