Validation

Published 4 months ago by Farouz

my question related to Validation : now i have $user->password this is the old password how i make a validation for input name='oldPassword' that the user must enter the the old password when reset his password

Dry7
Dry7
4 months ago (151,680 XP)

@Farouz

$validator = Validator::make($request->all(), [
    'oldPassword' => [
        'required',
        'max:255',
        function($attribute, $value, $fail) use ($user) {
            if (bcrypt($value) !== $user->password) {
                return $fail($attribute.' is invalid.');
            }
        },
    ],
]);
skliche
skliche
4 months ago (149,590 XP)

In the boot() method of your AppServiceProvider create a validPassword validator:

Validator::extend('validPassword', function($attribute, $value, $parameters) {
    return Hash::check($value, Auth::user()->getAttribute('password'));
});

Then you can use it in your rules array like this:

"current_password" => "required|validPassword",

And customize the error message in /resources/lang/en/validation.php:

'valid_password' => 'The supplied password is invalid.',

Please sign in or create an account to participate in this conversation.