You are here

public function SimpleForm::validateForm in Examples for Developers 8

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

Implements form validation.

The validateForm method is the default method called to validate input on a form.

Parameters

array $form: The render array of the currently built form.

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

Overrides FormBase::validateForm

File

form_api_example/src/Form/SimpleForm.php, line 87

Class

SimpleForm
Implements the SimpleForm form controller.

Namespace

Drupal\form_api_example\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $title = $form_state
    ->getValue('title');
  if (strlen($title) < 5) {

    // Set an error for the form element with a key of "title".
    $form_state
      ->setErrorByName('title', $this
      ->t('The title must be at least 5 characters long.'));
  }
}