Search code examples
laravellaravel-nova

Password Confirmation not working correctly in Laravel Nova


I am working on setting up a password confirmation field in my Laravel Nova resource.

In the AppServiceProvider.php I have:

use Illuminate\Validation\Rules\Password;

 Password::defaults(function () {
            return Password::min(8)
                ->letters()
                ->numbers()
                ->symbols()
                ->mixedCase();
        });

In my User.php I have:

Password::make(__('Password'))
                ->onlyOnForms()
                ->creationRules('required', Rules\Password::defaults())
                ->updateRules('nullable', Rules\Password::defaults()),

PasswordConfirmation::make('Password Confirmation'),

However, with these fields I am able to enter two different passwords and still submit the form.


Solution

  • Jon I hope you are having a good day :3

    I see that you implement the password validation without the confirmed field which ensures that you have defined the same password in both the password and password_confirmation fields

    1. Here is a snippet of how you can implement the confirmed parameter into your user.php.
    Password::make(__('Password'))
        ->onlyOnForms()
        ->creationRules('required', 'confirmed', Rules\Password::defaults())
        ->updateRules('nullable', 'confirmed', Rules\Password::defaults()),
    Password::make(__('Password Confirmation'), 'password_confirmation')
        ->onlyOnForms()
        ->creationRules('required')
        ->updateRules('nullable'),
    

    The creationRules('required') ensures both inputs are required during the creation.

    1. Ensure that AppServiceProvider correctly sets the default password rules as you provided in your code.

    2. Make sure that your form input for the password confirmation field input is named "password_confirmation" Laravel will automatically match it with the confirmed rule that you applied in the password field

    name="password_confirmation"
    

    I hope it works well for you, if you have any questions feel free to reply here!