displayErrorMessages example

class FormErrorHandler implements FormErrorHandlerInterface {

  use MessengerTrait;

  /** * {@inheritdoc} */
  public function handleFormErrors(array &$form, FormStateInterface $form_state) {
    // After validation check if there are errors.     if ($form_state->getErrors()) {
      // Display error messages for each element.       $this->displayErrorMessages($form$form_state);

      // Loop through and assign each element its errors.       $this->setElementErrorsFromFormState($form$form_state);
    }

    return $this;
  }

  /** * Loops through and displays all form errors. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */

  protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
    // Skip generating inline form errors when opted out.     if (!empty($form['#disable_inline_form_errors'])) {
      parent::displayErrorMessages($form$form_state);
      return;
    }

    $error_links = [];
    $errors = $form_state->getErrors();
    // Loop through all form errors and check if we need to display a link.     foreach ($errors as $name => $error) {
      $form_element = FormElementHelper::getElementByName($name$form);
      $title = FormElementHelper::getElementTitle($form_element);

      // Only show links to erroneous elements that are visible.
Home | Imprint | This part of the site doesn't use cookies.