I'm building a Laravel 8 app that requires user registration. As part of the registration, I need users to provide strong passwords (minimum characters, both lower and upper case letters, numbers and symbols).
Also, for convenience, the rules for the signup request are stored in the database, which means I cannot provide the above rules as object, for example: ['required', Password::min(8)->letters()->mixedCase()->numbers()->symbols()]
, only as a string: 'required,strong_password'
. To do so, I extended Validator with the above rule:
Validator::extend('strong_password', Password::min(8)
->letters()
->mixedCase()
->numbers()
->symbols());
All works well but, when the validation fails, it only provides one error message (validation.strong_password
, which I can translate).
I want to be able to take advantage of all the messages provided by the Password rule. These are available using the message()
method of the class, which is never called, because that happens only when the rule is passed as an object that instances Rule (see vendor/laravel/framework/src/Illuminate/Validation/Validator.php:562):
if ($rule instanceof RuleContract) {
return $validatable
? $this->validateUsingCustomRule($attribute, $value, $rule)
: null;
}
protected function validateUsingCustomRule($attribute, $value, $rule) {
// some code
$messages = $rule->message();
// mode code
}
The question is: how can I register the custom rule and store it in database as a string in order to benefit from its message()
method that is only called when the rule is passed as object?