You are here

public function MasqueradeForm::validateForm in Masquerade 8.2

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

src/Form/MasqueradeForm.php, line 92

Class

MasqueradeForm
Form builder for the masquerade form.

Namespace

Drupal\masquerade\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $user_id = $form_state
    ->getValue('masquerade_as');
  if (empty($user_id)) {
    $form_state
      ->setErrorByName('masquerade_as', $this
      ->t('The user does not exist. Please enter a valid username.'));
    return;
  }
  $target_account = $this->entityTypeManager
    ->getStorage('user')
    ->load($user_id);
  if ($error = masquerade_switch_user_validate($target_account)) {
    $form_state
      ->setErrorByName('masquerade_as', $error);
  }
  else {
    $form_state
      ->setValue('masquerade_target_account', $target_account);
  }
}