function FormTestValidateForm::validateForm

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php \Drupal\form_test\Form\FormTestValidateForm::validateForm()
  2. 8.9.x core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php \Drupal\form_test\Form\FormTestValidateForm::validateForm()
  3. 10 core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php \Drupal\form_test\Form\FormTestValidateForm::validateForm()

Overrides FormBase::validateForm

File

core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php, line 55

Class

FormTestValidateForm
Form builder for testing <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormValidatorInterface.php/function/FormValidatorInterface%3A%3AvalidateForm/11.x" title="Validates user-submitted form data in the $form_state." class="local">\Drupal\Core\Form\FormValidatorInterface::validateForm</a>().

Namespace

Drupal\form_test\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state->getValue('name') == 'validate') {
        // Alter the form element.
        $form['name']['#value'] = '#value changed by #validate';
        // Alter the submitted value in $form_state.
        $form_state->setValueForElement($form['name'], 'value changed by setValueForElement() in #validate');
        // Output the element's value from $form_state.
        $this->messenger()
            ->addStatus($this->t('@label value: @value', [
            '@label' => $form['name']['#title'],
            '@value' => $form_state->getValue('name'),
        ]));
        // Trigger a form validation error to see our changes.
        $form_state->setErrorByName('');
        // To simplify this test, enable form caching and use form storage to
        // remember our alteration.
        $form_state->setCached();
    }
}

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