You are here

public function UserPasswordForm::validateForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Form/UserPasswordForm.php \Drupal\user\Form\UserPasswordForm::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 FormBase::validateForm

File

core/modules/user/src/Form/UserPasswordForm.php, line 114
Contains \Drupal\user\Form\UserPasswordForm.

Class

UserPasswordForm
Provides a user password reset form.

Namespace

Drupal\user\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $name = trim($form_state
    ->getValue('name'));

  // Try to load by email.
  $users = $this->userStorage
    ->loadByProperties(array(
    'mail' => $name,
  ));
  if (empty($users)) {

    // No success, try to load by name.
    $users = $this->userStorage
      ->loadByProperties(array(
      'name' => $name,
    ));
  }
  $account = reset($users);
  if ($account && $account
    ->id()) {

    // Blocked accounts cannot request a new password.
    if (!$account
      ->isActive()) {
      $form_state
        ->setErrorByName('name', $this
        ->t('%name is blocked or has not been activated yet.', array(
        '%name' => $name,
      )));
    }
    else {
      $form_state
        ->setValueForElement(array(
        '#parents' => array(
          'account',
        ),
      ), $account);
    }
  }
  else {
    $form_state
      ->setErrorByName('name', $this
      ->t('%name is not recognized as a username or an email address.', array(
      '%name' => $name,
    )));
  }
}