You are here

public static function PasswordConfirm::validatePasswordConfirm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Render/Element/PasswordConfirm.php \Drupal\Core\Render\Element\PasswordConfirm::validatePasswordConfirm()

Validates a password_confirm element.

File

core/lib/Drupal/Core/Render/Element/PasswordConfirm.php, line 102
Contains \Drupal\Core\Render\Element\PasswordConfirm.

Class

PasswordConfirm
Provides a form element for double-input of passwords.

Namespace

Drupal\Core\Render\Element

Code

public static function validatePasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
  $pass1 = trim($element['pass1']['#value']);
  $pass2 = trim($element['pass2']['#value']);
  if (strlen($pass1) > 0 || strlen($pass2) > 0) {
    if (strcmp($pass1, $pass2)) {
      $form_state
        ->setError($element, t('The specified passwords do not match.'));
    }
  }
  elseif ($element['#required'] && $form_state
    ->getUserInput()) {
    $form_state
      ->setError($element, t('Password field is required.'));
  }

  // Password field must be converted from a two-element array into a single
  // string regardless of validation results.
  $form_state
    ->setValueForElement($element['pass1'], NULL);
  $form_state
    ->setValueForElement($element['pass2'], NULL);
  $form_state
    ->setValueForElement($element, $pass1);
  return $element;
}