protected function FormErrorHandler::setElementErrorsFromFormState

protected FormErrorHandler::setElementErrorsFromFormState(array &$elements, FormStateInterface &$form_state)

Stores the errors of each element directly on the element.

We must provide a way for non-form functions to check the errors for a specific element. The most common usage of this is a #pre_render callback.

Parameters

array $elements: An associative array containing the structure of a form element.

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

File

core/lib/Drupal/Core/Form/FormErrorHandler.php, line 56

Class

FormErrorHandler
Handles form errors.

Namespace

Drupal\Core\Form

Code

protected function setElementErrorsFromFormState(array &$elements, FormStateInterface &$form_state) {
  // Recurse through all children.
  foreach (Element::children($elements) as $key) {
    if (isset($elements[$key]) && $elements[$key]) {
      $this->setElementErrorsFromFormState($elements[$key], $form_state);
    }
  }

  // Store the errors for this element on the element directly.
  $elements['#errors'] = $form_state->getError($elements);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormErrorHandler.php/function/FormErrorHandler::setElementErrorsFromFormState/8.1.x