diff --git a/core/includes/form.inc b/core/includes/form.inc index 1658e8c..c4326d3 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -351,30 +351,6 @@ function template_preprocess_form(&$variables) { } $variables['attributes'] = $element['#attributes']; $variables['children'] = $element['#children']; - - if (!empty($element['#errors'])) { - $error_links = []; - // Loop through all form errors, and display a link for each error that - // is associated with a visible form element. - foreach ($element['#errors'] as $key => $error) { - if (($form_element = FormElementHelper::getElementByName($key, $element)) && Element::isVisibleElement($form_element)) { - $title = FormElementHelper::getElementTitle($form_element); - $error_links[] = \Drupal::l($title, Url::fromRoute('', [], ['fragment' => 'edit-' . str_replace('_', '-', $key), 'external' => TRUE])); - } - else { - drupal_set_message($error, 'error'); - } - } - - if (!empty($error_links)) { - // We need to pass this through String::format() so drupal_set_message() - // doesn't encode the links. - $message = \Drupal::translation()->formatPlural(count($error_links), '1 error has been found: !errors', '@count errors have been found: !errors', [ - '!errors' => implode(', ', $error_links), - ]); - drupal_set_message($message, 'error'); - } - } } /** diff --git a/core/lib/Drupal/Core/Form/FormValidator.php b/core/lib/Drupal/Core/Form/FormValidator.php index 0f2c23f..cddf4e0 100644 --- a/core/lib/Drupal/Core/Form/FormValidator.php +++ b/core/lib/Drupal/Core/Form/FormValidator.php @@ -13,6 +13,7 @@ use Drupal\Core\Render\Element; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\Url; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -188,11 +189,44 @@ protected function finalizeValidation(&$form, FormStateInterface &$form_state, $ $this->setElementErrorsFromFormState($form, $form_state); // Store all of the errors for this form at the top level. $form['#errors'] = $form_state->getErrors(); + $this->setErrors($form); + // Mark this form as validated. $form_state->setValidationComplete(); } /** + * Set error message to form elements. + * + * @param $element + */ + protected function setErrors($element) { + if (!empty($element['#errors'])) { + $error_links = []; + // Loop through all form errors, and display a link for each error that + // is associated with a visible form element. + foreach ($element['#errors'] as $key => $error) { + if (($form_element = FormElementHelper::getElementByName($key, $element)) && Element::isVisibleElement($form_element)) { + $title = FormElementHelper::getElementTitle($form_element); + $error_links[] = \Drupal::l($title, Url::fromRoute('', [], ['fragment' => 'edit-' . str_replace('_', '-', $key), 'external' => TRUE])); + } + else { + drupal_set_message($error, 'error'); + } + } + + if (!empty($error_links)) { + // We need to pass this through String::format() so drupal_set_message() + // doesn't encode the links. + $message = \Drupal::translation()->formatPlural(count($error_links), '1 error has been found: !errors', '@count errors have been found: !errors', [ + '!errors' => implode(', ', $error_links), + ]); + drupal_set_message($message, 'error'); + } + } + } + + /** * Performs validation on form elements. * * First ensures required fields are completed, #maxlength is not exceeded,