You are here

public function PasswordLength::validate in Password Policy 8.3

Returns a true/false status if the password meets the constraint.

Parameters

string $password: The password entered by the end user.

\Drupal\user\UserInterface: The user which password is changed.

Return value

PasswordPolicyValidation Whether or not the password meets the constraint in the plugin.

Overrides PasswordConstraintInterface::validate

File

password_policy_length/src/Plugin/PasswordConstraint/PasswordLength.php, line 25

Class

PasswordLength
Enforces a specific character length for passwords.

Namespace

Drupal\password_policy_length\Plugin\PasswordConstraint

Code

public function validate($password, UserInterface $user) {
  $configuration = $this
    ->getConfiguration();
  $validation = new PasswordPolicyValidation();
  switch ($configuration['character_operation']) {
    case 'minimum':
      if (strlen($password) < $configuration['character_length']) {
        $validation
          ->setErrorMessage($this
          ->formatPlural($configuration['character_length'], 'Password length must be at least 1 character.', 'Password length must be at least @count characters.'));
      }
      break;
    case 'maximum':
      if (strlen($password) > $configuration['character_length']) {
        $validation
          ->setErrorMessage($this
          ->formatPlural($configuration['character_length'], 'Password length must not exceed 1 character.', 'Password length must not exceed @count characters.'));
      }
      break;
  }
  return $validation;
}