Laravel Password & Password_Confirmation Validation

LaravelValidationPasswords

Laravel Problem Overview


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.

image

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?

Laravel Solutions


Solution 1 - Laravel

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',
]);

Solution 2 - Laravel

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

Solution 3 - Laravel

Try this:

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

Solution 4 - Laravel

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',
    ]);

Solution 5 - Laravel

I have used in this way.. Working fine!

 $inputs = request()->validate([
		'name' => 'required | min:6 | max: 20',
		'email' => 'required',
		'password' => 'required| min:4| max:7 |confirmed',
		'password_confirmation' => 'required| min:4'
  ]);

Solution 6 - Laravel

I have used in this way. Its Working fine!

$rules = [

            'password' => [

                    'required',

                    'string',

                    'min:6',

                'max:12',             // must be at least 8 characters in length

                ],

            'confirm_password' => 'required|same:password|min:6'

        ];

Solution 7 - Laravel

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

Solution 8 - Laravel

Very easy in Laravel 9

For Laravel 9.x, here is the link: https://laravel.com/docs/9.x/validation#rule-confirmed , and below code is what works for me:

public function store(){
    $signUp = request()->validate([
        'student_email' => 'required|email|max:255',
        *'password' => 'required|confirmed|min:7|max:255',*
    ]);

Notes:

In your form.blade, ensure that the password input field has a name attribute of name="password" -> it has to be password ,

  • Also, add an attribute of name="password_confirmation" to the Confirm Password text input box, and it will work.

  • This reduces the code in the form request validation method of the store.

  • and there was no need to add Password_confirmation name attributes to your validation request method in the controller.

Cheers...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxTheWolfView Question on Stackoverflow
Solution 1 - LaravelNeabfiView Answer on Stackoverflow
Solution 2 - LaravelomeataiView Answer on Stackoverflow
Solution 3 - LaravelOdin ThunderView Answer on Stackoverflow
Solution 4 - LaraveldparoliView Answer on Stackoverflow
Solution 5 - LaravelDEBARSHI MONDALView Answer on Stackoverflow
Solution 6 - Laravelhammad khanView Answer on Stackoverflow
Solution 7 - LaravelLuca C.View Answer on Stackoverflow
Solution 8 - LaravelDhikrullahView Answer on Stackoverflow