Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Make password rule errors translatable #42060

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/Illuminate/Validation/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,31 @@ public function passes($attribute, $value)
$value = (string) $value;

if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one uppercase and one lowercase letter.');
$validator->errors()->add(
reziamini marked this conversation as resolved.
Show resolved Hide resolved
$attribute,
$this->getErrorMessage('validation.password.mixed')
);
}

if ($this->letters && ! preg_match('/\pL/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one letter.');
$validator->errors()->add(
$attribute,
$this->getErrorMessage('validation.password.letters')
);
}

if ($this->symbols && ! preg_match('/\p{Z}|\p{S}|\p{P}/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one symbol.');
$validator->errors()->add(
$attribute,
$this->getErrorMessage('validation.password.symbols')
);
}

if ($this->numbers && ! preg_match('/\pN/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one number.');
$validator->errors()->add(
$attribute,
$this->getErrorMessage('validation.password.numbers')
);
}
});

Expand All @@ -327,9 +339,7 @@ public function passes($attribute, $value)
'value' => $value,
'threshold' => $this->compromisedThreshold,
])) {
return $this->fail(
'The given :attribute has appeared in a data leak. Please choose a different :attribute.'
);
return $this->fail($this->getErrorMessage('validation.password.uncompromised'));
}

return true;
Expand All @@ -345,6 +355,29 @@ public function message()
return $this->messages;
}

/**
* Get the translated password error message.
*
* @param string $key
* @return string
*/
protected function getErrorMessage($key)
{
$messages = [
'validation.password.mixed' => 'The :attribute must contain at least one uppercase and one lowercase letter.',
'validation.password.letters' => 'The :attribute must contain at least one letter.',
'validation.password.symbols' => 'The :attribute must contain at least one symbol.',
'validation.password.numbers' => 'The :attribute must contain at least one number.',
'validation.password.uncompromised' => 'The given :attribute has appeared in a data leak. Please choose a different :attribute.',
];

if (($message = $this->validator->getTranslator()->get($key)) !== $key) {
return $message;
}

return $messages[$key];
reziamini marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Adds the given failures, and return false.
*
Expand Down