Laravel 密码 & Password_Confirmation 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42623841/
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-08 17:40:38  来源:igfitidea点击:

Laravel Password & Password_Confirmation Validation

laravelvalidationpasswords

提问by Scarwolf

I've been using this in order to edit the User Account Info:

我一直在使用它来编辑用户帐户信息:

$this->validate($request, [
    'password' => 'min:6',
    'password_confirmation' => 'required_with:password|same:password|min:6'
]);

This worked fine in a Laravel 5.2 Application but does not work in a 5.4 Application.

这在 Laravel 5.2 应用程序中运行良好,但在 5.4 应用程序中不起作用。

image

图片

What's wrong here, or what is the correct way in order to only make the passwordrequired if either the passwordor password_confirmationfield is set?

这里有什么问题,或者只有在设置or字段时才password需要的正确方法是什么?passwordpassword_confirmation

回答by Neabfi

You can use the confirmedvalidation rule.

您可以使用已确认的验证规则。

$this->validate($request, [
    'name' => 'required|min:3|max:50',
    'email' => 'email',
    'vat_number' => 'max:13',
    'password' => 'required|confirmed|min:6',
]);

回答by omeati

Try doing it this way, it worked for me:

尝试这样做,它对我有用:

$this->validate($request, [
'name' => 'required|min:3|max:50',
'email' => 'email',
'vat_number' => 'max:13',
'password' => 'min:6|required_with:password_confirmation|same:password_confirmation',
'password_confirmation' => 'min:6'
]);`

Seems like the rule always has the validation on the first input among the pair...

似乎规则总是对配对中的第一个输入进行验证......

回答by dparoli

try confirmed and without password_confirmation rule:

尝试确认和没有 password_confirmation 规则:

$this->validate($request, [
        'name' => 'required|min:3|max:50',
        'email' => 'email',
        'vat_number' => 'max:13',
        'password' => 'confirmed|min:6',
    ]);

回答by Odin Thunder

Try this:

尝试这个:

'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6'

回答by Luca C.

It should be enough to do:

这样做应该足够了:

$this->validate($request, [
    'password' => 'sometimes,min:6,confirmed,required_with:password_confirmed',
]);

Make password optional, but if present requires a password_confirmation that matches, also make password required only if password_confirmed is present

将密码设为可选,但如果存在需要匹配的 password_confirmation,也仅当存在 password_confirmed 时才需要密码