W3cubDocs

/Drupal 8

public static function PasswordConfirm::validatePasswordConfirm

public static PasswordConfirm::validatePasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form)

Validates a password_confirm element.

File

core/lib/Drupal/Core/Render/Element/PasswordConfirm.php, line 97

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!PasswordConfirm.php/function/PasswordConfirm::validatePasswordConfirm/8.1.x