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
Laravel Password & Password_Confirmation Validation
提问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 应用程序中不起作用。
What's wrong here, or what is the correct way in order to only make the password
required if either the password
or password_confirmation
field is set?
这里有什么问题,或者只有在设置or字段时才password
需要的正确方法是什么?password
password_confirmation
回答by Neabfi
回答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 时才需要密码