在 Laravel 中,如果不满足任何密码复杂性标准,如何显示自定义错误消息?

问题描述 投票:0回答:1

我正在 Laravel 8 中开发博客应用程序

我使用了 Laravel 8 提供的标准程序来确保密码满足复杂性标准:

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', 'min:6'],
        '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);
}

目的是在不满足任何复杂性标准时显示(验证)错误消息“包括大写和小写字母,至少一个数字和一个符号”。

我希望能够将所有复杂性标准“合并”到一条消息中。

但是预想的事情并没有发生。相反,会显示标准消息,每个失败的标准都会显示一条消息。

我的错误是什么?

php laravel laravel-8
1个回答
0
投票
'password.*' => 'your merged message here'
© www.soinside.com 2019 - 2024. All rights reserved.