function ConfigFormBase::formatMultipleViolationsMessage

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Form/ConfigFormBase.php \Drupal\Core\Form\ConfigFormBase::formatMultipleViolationsMessage()

Formats multiple violation messages associated with a single form element.

Validation constraints only know the internal data structure (the configuration schema structure), but this need not be a disadvantage: rather than informing the user some values are wrong, it is possible guide them directly to the Nth entry in the sequence.

To further improve the user experience, it is possible to override method in subclasses to use specific knowledge about the structure of the form and the nature of the data being validated, to instead generate more precise and/or shortened violation messages.

Parameters

string $form_element_name: The form element for which to format multiple violation messages.

\Symfony\Component\Validator\ConstraintViolationListInterface $violations: The list of constraint violations that apply to this form element.

Return value

\Drupal\Core\StringTranslation\TranslatableMarkup

2 calls to ConfigFormBase::formatMultipleViolationsMessage()
ConfigFormBase::validateForm in core/lib/Drupal/Core/Form/ConfigFormBase.php
Form validation handler.
UpdateSettingsForm::formatMultipleViolationsMessage in core/modules/update/src/UpdateSettingsForm.php
Formats multiple violation messages associated with a single form element.
1 method overrides ConfigFormBase::formatMultipleViolationsMessage()
UpdateSettingsForm::formatMultipleViolationsMessage in core/modules/update/src/UpdateSettingsForm.php
Formats multiple violation messages associated with a single form element.

File

core/lib/Drupal/Core/Form/ConfigFormBase.php, line 284

Class

ConfigFormBase
Base class for implementing system configuration forms.

Namespace

Drupal\Core\Form

Code

protected function formatMultipleViolationsMessage(string $form_element_name, array $violations) : TranslatableMarkup {
    $transformed_message_parts = [];
    foreach ($violations as $index => $violation) {
        // Note that `@validation_error_message` (should) already contain a
        // trailing period, hence it is intentionally absent here.
        $transformed_message_parts[] = $this->t('Entry @human_index: @validation_error_message', [
            // Humans start counting from 1, not 0.
'@human_index' => $index + 1,
            // Translators may not necessarily know what "violation constraint
            // messages" are, but they definitely know "validation errors".
'@validation_error_message' => $violation->getMessage(),
        ]);
    }
    return $this->t(implode("\n", $transformed_message_parts));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.