class FormErrorHandler extends FormErrorHandler (View source)

Produces inline form errors.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Provides a trait for the messenger service.

Properties

protected MessengerInterface $messenger

The messenger.

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected RendererInterface $renderer

The renderer service.

Methods

setMessenger(MessengerInterface $messenger)

Sets the messenger.

messenger()

Gets the messenger.

$this
handleFormErrors(array $form, FormStateInterface $form_state)

Handles form errors after form validation.

displayErrorMessages(array $form, FormStateInterface $form_state)

Loops through and displays all form errors.

setElementErrorsFromFormState(array $form, FormStateInterface $form_state, array $elements = [])

Stores errors and a list of child element errors directly on each element.

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__construct(TranslationInterface $string_translation, RendererInterface $renderer, MessengerInterface $messenger)

Constructs a new FormErrorHandler.

Details

setMessenger(MessengerInterface $messenger)

Sets the messenger.

Parameters

MessengerInterface $messenger

The messenger.

MessengerInterface messenger()

Gets the messenger.

Return Value

MessengerInterface

The messenger.

$this handleFormErrors(array $form, FormStateInterface $form_state)

Handles form errors after form validation.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

Return Value

$this

protected displayErrorMessages(array $form, FormStateInterface $form_state)

Loops through and displays all form errors.

To disable inline form errors for an entire form set the

disable_inline_form_errors property to TRUE on the top level of the $form

array:

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

protected setElementErrorsFromFormState(array $form, FormStateInterface $form_state, array $elements = [])

Stores errors and a list of child element errors directly on each element.

Grouping elements like containers, details, fieldgroups and fieldsets may need error info from their child elements to be able to accessibly show form error messages to a user. For example, a details element should be opened when child elements have errors.

Grouping example: Assume you have a 'street' element somewhere in a form, which is displayed in a details element 'address'. It might be:

Parameters

array $form

An associative array containing a reference to the complete structure of the form.

FormStateInterface $form_state

The current state of the form.

array $elements

An associative array containing the part of the form structure that will be processed while traversing up the tree. For recursion only; leave empty when calling this method.

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__construct(TranslationInterface $string_translation, RendererInterface $renderer, MessengerInterface $messenger)

Constructs a new FormErrorHandler.

Parameters

TranslationInterface $string_translation

The string translation service.

RendererInterface $renderer

The renderer service.

MessengerInterface $messenger

The messenger.