You are here

public function DbtngExampleAddForm::validateForm in Examples for Developers 8

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

Form validation handler.

Parameters

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

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

Overrides FormInterface::validateForm

File

dbtng_example/src/Form/DbtngExampleAddForm.php, line 114

Class

DbtngExampleAddForm
Form to add a database entry, with all the interesting fields.

Namespace

Drupal\dbtng_example\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Verify that the user is logged-in.
  if ($this->currentUser
    ->isAnonymous()) {
    $form_state
      ->setError($form['add'], $this
      ->t('You must be logged in to add values to the database.'));
  }

  // Confirm that age is numeric.
  if (!intval($form_state
    ->getValue('age'))) {
    $form_state
      ->setErrorByName('age', $this
      ->t('Age needs to be a number'));
  }
}