function FormValidator::performRequiredValidation

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Form/FormValidator.php \Drupal\Core\Form\FormValidator::performRequiredValidation()
  2. 8.9.x core/lib/Drupal/Core/Form/FormValidator.php \Drupal\Core\Form\FormValidator::performRequiredValidation()
  3. 10 core/lib/Drupal/Core/Form/FormValidator.php \Drupal\Core\Form\FormValidator::performRequiredValidation()

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

Parameters

array $elements: An associative array containing the structure of the form.

\Drupal\Core\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.

1 call to FormValidator::performRequiredValidation()
FormValidator::doValidateForm in core/lib/Drupal/Core/Form/FormValidator.php
Performs validation on form elements.

File

core/lib/Drupal/Core/Form/FormValidator.php, line 330

Class

FormValidator
Provides validation of form submissions.

Namespace

Drupal\Core\Form

Code

protected function performRequiredValidation(&$elements, FormStateInterface &$form_state) {
    // Verify that the value is not longer than #maxlength.
    if (isset($elements['#maxlength']) && mb_strlen($elements['#value']) > $elements['#maxlength']) {
        $form_state->setError($elements, $this->t('@name cannot be longer than %max characters but is currently %length characters long.', [
            '@name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
            '%max' => $elements['#maxlength'],
            '%length' => mb_strlen($elements['#value']),
        ]));
    }
    if (isset($elements['#options']) && isset($elements['#value'])) {
        $name = empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'];
        $message_arguments = [
            '%name' => $name,
        ];
        if ($elements['#type'] == 'select') {
            $options = OptGroup::flattenOptions($elements['#options']);
        }
        else {
            $options = $elements['#options'];
        }
        if (is_array($elements['#value'])) {
            $value = in_array($elements['#type'], [
                'checkboxes',
                'tableselect',
            ]) ? array_keys($elements['#value']) : $elements['#value'];
            foreach ($value as $v) {
                if (!isset($options[$v])) {
                    $message_arguments['%choice'] = $v;
                    $form_state->setError($elements, $this->t('The submitted value %choice in the %name element is not allowed.', $message_arguments));
                    $this->logger
                        ->error('The submitted value %choice in the %name element is not allowed.', $message_arguments);
                }
            }
        }
        elseif ($elements['#type'] == 'select' && !$elements['#multiple'] && $elements['#required'] && !isset($elements['#default_value']) && $elements['#value'] === $elements['#empty_value']) {
            $elements['#value'] = NULL;
            $form_state->setValueForElement($elements, NULL);
        }
        elseif (!isset($options[$elements['#value']])) {
            $message_arguments['%choice'] = $elements['#value'];
            $form_state->setError($elements, $this->t('The submitted value %choice in the %name element is not allowed.', $message_arguments));
            $this->logger
                ->error('The submitted value %choice in the %name element is not allowed.', $message_arguments);
        }
    }
}

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