如何让 Laravel 'confirmed' 验证器向确认字段添加错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30170394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:31:42  来源:igfitidea点击:

How to make Laravel 'confirmed' validator to add errors to the confirmation field?

phpvalidationlaravel

提问by JustAMartin

By default, Laravel 'confirmed' validator adds the error message to the original field and not to the field which usually contains the confirmed value.

默认情况下,Laravel 'confirmed' 验证器将错误消息添加到原始字段而不是通常包含确认值的字段。

'password' => 'required|confirmed|min:8',

Is there any simple way to extend the validator or use some trick to force it to always show the error on the confirmation field instead of the original field?

有没有简单的方法来扩展验证器或使用一些技巧来强制它始终在确认字段而不是原始字段上显示错误?

If I fail to enter my password twice, the error seems more appropriate to belong the confirmation field and not to the original password field. Or maybe that's just our UX analyst getting nitpicky...

如果我两次输入密码失败,错误似乎更适合属于确认字段而不是原始密码字段。或者也许这只是我们的 UX 分析师变得挑剔...

回答by peterm

One way to go about it is to use samerule instead of confirmed

一种方法是使用same规则而不是confirmed

// ...

$input = Input::all();

$rules = [
    'password' => 'required|min:8',
    'password_confirmation' => 'required|min:8|same:password',
];

$messages = [
    'password_confirmation.same' => 'Password Confirmation should match the Password',
];
$validator = Validator::make($input, $rules, $messages);

if ($validator->fails()) {
    return back()->withInput()->withErrors($validator->messages());
}
// ...

回答by Tuncay Elvana?a?

You should design your form as below;

你应该设计你的表格如下;

<input type="password" name="password">
<input type="password" name="password_confirmation">

Quote from Laravel: confirmed"The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input"

来自 Laravel 的引用:已确认“验证中的字段必须具有匹配的 foo_confirmation 字段。例如,如果验证中的字段是密码,则输入中必须存在匹配的 password_confirmation 字段”

Now, you can design your validation as follow;

现在,您可以按如下方式设计验证;

$request->validate([
        "password" => 'required|confirmed' 
    ]);

回答by user1669496

One solution that quickly comes to mind is to just display the passworderrors on the password_confirmationfield.

很快想到的一种解决方案是仅passwordpassword_confirmation现场显示错误。

If that won't work for you, just label the password_confirmationfield as password and the passwordfield as password confirmation so that if there are errors, it will show up near the password_confirmationlabel rather than the passwordlabel.

如果这对您不起作用,只需将该password_confirmation字段标记为密码并将该password字段标记为密码确认,这样如果出现错误,它就会显示在password_confirmation标签附近而不是password标签附近。

Otherwise, it's not difficult to add a your own custom validation method.

否则,添加您自己的自定义验证方法并不困难。

回答by Ahmed Mahmoud

$rules=[
            'username'=>'required|max:20',
            'password1'=>'required|min:8',
            'password2'=>'required|min:8|same:password1',
        ];
        $error_messages=[
            'password2.same'=>'password are not the same password must match same value',
            'password1.min'=>'password length must be greater than 8 characters',
            'password2.min'=>'confirm-password length must be greater than 8 characters',


        ];
        $validator=  validator($request->all(), $rules, $error_messages);
        if ($validator->fails()) {
            return redirect('control_pannel/change_password')
                        ->withErrors($validator)
                        ->withInput();
       }