Laravel Password & Password_Confirmation Validation

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?

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

1

Try this:

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

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

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

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'

        ];

Very easy in Laravel 9

For Laravel 9.x, here is the link: , 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...

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

Note when using the second code you will have to import the Rule class Also added the student_class just to show you how to call the unique in both cases

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

// this is another way you can write it and it will still run

public function store(Request $request){
$signUp = $request->validate([
    'student_email' => ['required', 'email', 'max:255'],
    'student_class' => ['required', Rule::unique('student', 'student_class')],
    'password' => ['required', 'confirmed', 'min:7', 'max:255']
]);
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest