You are here

public function MassPasswordResetForm::validateForm in Mass Password Reset 8

Same name and namespace in other branches
  1. 2.x src/Form/MassPasswordResetForm.php \Drupal\mass_pwreset\Form\MassPasswordResetForm::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

src/Form/MassPasswordResetForm.php, line 109

Class

MassPasswordResetForm
Mass Password Reset Form.

Namespace

Drupal\mass_pwreset\Form

Code

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

  // Get the selected roles from the form.
  $selected_roles = array_filter($form_state
    ->getValue('selected_roles'));

  // User must select roles for mass password reset.
  if ($form_state
    ->getValue('authenticated_role') == 0 && empty($selected_roles)) {
    $form_state
      ->setErrorByName('authenticated_role', $this
      ->t('Please select all users or select specific roles.'));
    return;
  }

  // Holds all the user ids to have the passwords reset.
  $uids = [];

  // If reset for all users is checked, get all the uids - excluding the
  // current user id and user 1.
  if ($form_state
    ->getValue('authenticated_role') == 1) {
    $uids = mass_pwreset_get_uids();
    $roles = [
      'authenticated role',
    ];
  }
  else {
    $roles = $selected_roles;
    $uids = mass_pwreset_get_uids_by_selected_roles($roles);
  }

  // If there are no users returned and there are roles selected, set error.
  if (empty($uids) && $selected_roles) {
    $form_state
      ->setErrorByName('selected_roles', $this
      ->t('There are no users with the selected role.'));
  }

  // Set the 'uids' and 'roles' values for use in submitForm.
  $form_state
    ->set('uids', $uids);
  $form_state
    ->set('roles', $roles);
}