class FormValidator implements FormValidatorInterface (View source)

Provides validation of form submissions.

Traits

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

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected CsrfTokenGenerator $csrfToken

The CSRF token generator to validate the form token.

protected RequestStack $requestStack

The request stack.

protected LoggerInterface $logger

A logger instance.

protected FormErrorHandlerInterface $formErrorHandler

The form error handler.

Methods

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(RequestStack $request_stack, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, FormErrorHandlerInterface $form_error_handler)

Constructs a new FormValidator.

executeValidateHandlers($form, FormStateInterface $form_state)

Executes custom validation handlers for a given form.

validateForm($form_id, $form, FormStateInterface $form_state)

Validates user-submitted form data in the $form_state.

$this
setInvalidTokenError(FormStateInterface $form_state)

Sets a form_token error on the given form state.

handleErrorsWithLimitedValidation(array $form, FormStateInterface $form_state, string $form_id)

Handles validation errors for forms with limited validation.

finalizeValidation(array $form, FormStateInterface $form_state, string $form_id)

Finalizes validation.

doValidateForm($elements, FormStateInterface $form_state, $form_id = NULL)

Performs validation on form elements.

performRequiredValidation(array $elements, FormStateInterface $form_state)

Performs validation of elements that are not subject to limited validation.

array|null
determineLimitValidationErrors(FormStateInterface $form_state)

Determines if validation errors should be limited.

Details

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(RequestStack $request_stack, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token, LoggerInterface $logger, FormErrorHandlerInterface $form_error_handler)

Constructs a new FormValidator.

Parameters

RequestStack $request_stack

The request stack.

TranslationInterface $string_translation

The string translation service.

CsrfTokenGenerator $csrf_token

The CSRF token generator.

LoggerInterface $logger

A logger instance.

FormErrorHandlerInterface $form_error_handler

The form error handler.

executeValidateHandlers($form, FormStateInterface $form_state)

Executes custom validation handlers for a given form.

Button-specific handlers are checked first. If none exist, the function falls back to form-level handlers.

Parameters

$form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form. If the user submitted the form by clicking a button with custom handler functions defined, those handlers will be stored here.

validateForm($form_id, $form, FormStateInterface $form_state)

Validates user-submitted form data in the $form_state.

Parameters

$form_id

A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.

$form

An associative array containing the structure of the form, which is passed by reference. Form validation handlers are able to alter the form structure (like #process and #after_build callbacks during form building) in case of a validation error. If a validation handler alters the form structure, it is responsible for validating the values of changed form elements in $form_state->getValues() to prevent form submit handlers from receiving unvalidated values.

FormStateInterface $form_state

The current state of the form. The current user-submitted data is stored in $form_state->getValues(), though form validation functions are passed an explicit copy of the values for the sake of simplicity. Validation handlers can also use $form_state to pass information on to submit handlers. For example: $form_state->set('data_for_submission', $data); This technique is useful when validation requires file parsing, web service requests, or other expensive requests that should not be repeated in the submission step.

$this setInvalidTokenError(FormStateInterface $form_state)

Sets a form_token error on the given form state.

Parameters

FormStateInterface $form_state

The current state of the form.

Return Value

$this

protected handleErrorsWithLimitedValidation(array $form, FormStateInterface $form_state, string $form_id)

Handles validation errors for forms with limited validation.

If validation errors are limited then remove any non validated form values, so that only values that passed validation are left for submit callbacks.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

string $form_id

The unique string identifying the form.

protected finalizeValidation(array $form, FormStateInterface $form_state, string $form_id)

Finalizes validation.

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

string $form_id

The unique string identifying the form.

protected doValidateForm($elements, FormStateInterface $form_state, $form_id = NULL)

Performs validation on form elements.

First ensures required fields are completed, #maxlength is not exceeded, and selected options were in the list of options given to the user. Then calls user-defined validators.

Parameters

$elements

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form. The current user-submitted data is stored in $form_state->getValues(), though form validation functions are passed an explicit copy of the values for the sake of simplicity. Validation handlers can also $form_state to pass information on to submit handlers. For example: $form_state->set('data_for_submission', $data); This technique is useful when validation requires file parsing, web service requests, or other expensive requests that should not be repeated in the submission step.

$form_id

A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.

protected performRequiredValidation(array $elements, FormStateInterface $form_state)

Performs validation of elements that are not subject to limited validation.

Parameters

array $elements

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form. The current user-submitted data is stored in $form_state->getValues(), though form validation functions are passed an explicit copy of the values for the sake of simplicity. Validation handlers can also $form_state to pass information on to submit handlers. For example: $form_state->set('data_for_submission', $data); This technique is useful when validation requires file parsing, web service requests, or other expensive requests that should not be repeated in the submission step.

protected array|null determineLimitValidationErrors(FormStateInterface $form_state)

Determines if validation errors should be limited.

Parameters

FormStateInterface $form_state

The current state of the form.

Return Value

array|null