skip to Main Content

I am working on a blogging application in Laravel 8.

I have used the standard procedure Laravel 8 provides to ensure the password meets the complexity criteria:

protected function validator(array $data)
{
    $rules = [
        'first_name' => ['required', 'string', 'max:255'],
        'last_name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'confirmed', Password::min(6)->mixedCase()->letters()->numbers()->symbols()],
        'password_confirmation' => ['required', 'same:password'],
        'accept' => ['accepted'],
    ];

    $messages = [
        'first_name.required' => 'The "First name" field is required',
        'last_name.required' => 'The "Last name" field is required',
        'email.required' => 'Please provide a valid email address',
        'email.email' => 'The email address you provided is not valid',
        'email.unique' => 'The email address you provided is already in use',
        'password.required' => 'A password is required',
        'password.mixedcase' => 'Include uppercase and lowercase letters, at least one number and one symbol',
        'password.letters' => 'Include uppercase and lowercase letters, at least one number and one symbol',
        'password.numbers' => 'Include uppercase and lowercase letters, at least one number and one symbol',
        'password.symbol' => 'Include uppercase and lowercase letters, at least one number and one symbol',
        'accept.required' => 'You must accept the Terms & conditions of service'
    ];
    
    return Validator::make($data, $rules, $messages);
}

The intention is to show the (validation) error message "Include uppercase and lowercase letters, at least one number and one symbol" if any of the complexity criteria is not met.

I would love to be able to "merge" all complexity criteria into one message. Instead of:

'password.mixedcase' => 'Include uppercase and lowercase letters, at least one number and one symbol',
'password.letters' => 'Include uppercase and lowercase letters, at least one number and one symbol',
'password.numbers' => 'Include uppercase and lowercase letters, at least one number and one symbol',
'password.symbol' => 'Include uppercase and lowercase letters, at least one number and one symbol'

I want to be able to use something like:

'password.isComplex' => 'Include uppercase and lowercase letters, at least one number and one symbol'

But the expected thing does not happen. Instead, the standard messages are displayed, one for each failed criterion.

UPDATE

Using

'password.required' => 'your password required message here',
'password.*' => 'Include uppercase and lowercase letters, at least one number and one symbol'

does not account for Password::min(6), so it does not work.

What is my mistake?

2

Answers


  1. Did you try

    'password.required' => 'your password required message here',
    'password.*' => 'Include uppercase and lowercase letters, at least one number and one symbol'
    
    Login or Signup to reply.
  2. You could try using ‘password’ => [‘required’, Password::defaults()] and then add a message for required and one for defaults like you did in the above example. Maybe this link in the docs can help: https://laravel.com/docs/8.x/validation#defining-default-password-rules. You would have a single rule defaults so just one message.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search