0

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.

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are correct, the confirmed parameter is required for creationRules and updateRules. Thank you. Also found the same answer at github.com/laravel/nova-issues/issues/154

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.