function FormValidator::performRequiredValidation

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Form/FormValidator.php \Drupal\Core\Form\FormValidator::performRequiredValidation()
  2. 10 core/lib/Drupal/Core/Form/FormValidator.php \Drupal\Core\Form\FormValidator::performRequiredValidation()
  3. 11.x 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 331

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'])) {
        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])) {
                    $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
                    $this->logger
                        ->error('Illegal choice %choice in %name element.', [
                        '%choice' => $v,
                        '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
                    ]);
                }
            }
        }
        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']])) {
            $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.'));
            $this->logger
                ->error('Illegal choice %choice in %name element.', [
                '%choice' => $elements['#value'],
                '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'],
            ]);
        }
    }
}

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