W3cubDocs

/Drupal 8

protected function FormValidator::handleErrorsWithLimitedValidation

protected FormValidator::handleErrorsWithLimitedValidation(&$form, FormStateInterface &$form_state, $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.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $form_id: The unique string identifying the form.

File

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

Class

FormValidator
Provides validation of form submissions.

Namespace

Drupal\Core\Form

Code

protected function handleErrorsWithLimitedValidation(&$form, FormStateInterface &$form_state, $form_id) {
  // If validation errors are limited then remove any non validated form values,
  // so that only values that passed validation are left for submit callbacks.
  $triggering_element = $form_state->getTriggeringElement();
  if (isset($triggering_element['#limit_validation_errors']) && $triggering_element['#limit_validation_errors'] !== FALSE) {
    $values = array();
    foreach ($triggering_element['#limit_validation_errors'] as $section) {
      // If the section exists within $form_state->getValues(), even if the
      // value is NULL, copy it to $values.
      $section_exists = NULL;
      $value = NestedArray::getValue($form_state->getValues(), $section, $section_exists);
      if ($section_exists) {
        NestedArray::setValue($values, $section, $value);
      }
    }
    // A button's #value does not require validation, so for convenience we
    // allow the value of the clicked button to be retained in its normal
    // $form_state->getValues() locations, even if these locations are not
    // included in #limit_validation_errors.
    if (!empty($triggering_element['#is_button'])) {
      $button_value = $triggering_element['#value'];

      // Like all input controls, the button value may be in the location
      // dictated by #parents. If it is, copy it to $values, but do not
      // override what may already be in $values.
      $parents = $triggering_element['#parents'];
      if (!NestedArray::keyExists($values, $parents) && NestedArray::getValue($form_state->getValues(), $parents) === $button_value) {
        NestedArray::setValue($values, $parents, $button_value);
      }

      // Additionally, self::doBuildForm() places the button value in
      // $form_state->getValue(BUTTON_NAME). If it's still there, after
      // validation handlers have run, copy it to $values, but do not override
      // what may already be in $values.
      $name = $triggering_element['#name'];
      if (!isset($values[$name]) && $form_state->getValue($name) === $button_value) {
        $values[$name] = $button_value;
      }
    }
    $form_state->setValues($values);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormValidator.php/function/FormValidator::handleErrorsWithLimitedValidation/8.1.x