function PasswordConfirm::validatePasswordConfirm

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/PasswordConfirm.php \Drupal\Core\Render\Element\PasswordConfirm::validatePasswordConfirm()
  2. 8.9.x core/lib/Drupal/Core/Render/Element/PasswordConfirm.php \Drupal\Core\Render\Element\PasswordConfirm::validatePasswordConfirm()
  3. 10 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 106

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.