Laravel Password & Password_Confirmation Validation

本秂侑毒 提交于 2019-12-18 10:39:20

问题


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.

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?


回答1:


You can use the confirmed validation rule.

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



回答2:


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




回答3:


try confirmed and without password_confirmation rule:

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



回答4:


Try this:

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



回答5:


It should be enough to do:

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

Make password optional, but if present requires a password_confirmation that matches



来源:https://stackoverflow.com/questions/42623841/laravel-password-password-confirmation-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!