You are here

public function MultistepForm::fapiExampleMultistepFormNextValidate in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/form_api_example/src/Form/MultistepForm.php \Drupal\form_api_example\Form\MultistepForm::fapiExampleMultistepFormNextValidate()

Provides custom validation handler for page 1.

Parameters

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

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

File

form_api_example/src/Form/MultistepForm.php, line 106

Class

MultistepForm
Provides a form with two steps.

Namespace

Drupal\form_api_example\Form

Code

public function fapiExampleMultistepFormNextValidate(array &$form, FormStateInterface $form_state) {
  $birth_year = $form_state
    ->getValue('birth_year');
  if ($birth_year != '' && ($birth_year < 1900 || $birth_year > 2000)) {

    // Set an error for the form element with a key of "birth_year".
    $form_state
      ->setErrorByName('birth_year', $this
      ->t('Enter a year between 1900 and 2000.'));
  }
}