phplaravellaravel-8

In Laravel, how can I display a custom error message if any of the password complexity criteria is not met?


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 work.

What is my mistake?


Solution

  • After trying out several options (thanks to everyone that helped!), my choice was using a regular expression:

    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', 'min:6', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).*$/'],
            '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.min' => 'The password must have at least :min characters',
            'password.regex' => 'Include uppercase and lowercase letters, at least one number and one symbol (special character)',
            'accept.required' => 'You must accept the Terms & conditions of service'
        ];
        
        return Validator::make($data, $rules, $messages);
    }