4

I am implementing a "Change Password" feature in my Laravel application. I already have the user's current password stored in the object $pass. I need to validate this $pass against the form input current_password to ensure the user entered their correct current password before allowing them to set a new password.

Here’s the validation rule I tried:

$rules = array('password_current' => "required|same:$pass");

Unfortunately, this doesn't work, and I get an error. How can I validate the user's input (current_password) against the stored password ($pass) using Laravel's validation system?

2 Answers 2

9

since same: used to ensure that the value of current field is the same as another field defined by the rule parameter (not object). so you can't use this function take a look this example code below.

$data = Input::all();
$rules = array(
    'email' => 'required|same:old_email',
);

the above code will check if current email field is same as old_email field. so i think you can you simple if else

in your handle controller function assume

public function handleCheck(){

$current_password = Input::get('current_password');
$pass = //your object pass;
if($current_password == $pass){
  // password correct , show change password form
}else{
 //  password incorrect , show error
}
}

let me know if it works. see Laravel Validation same

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

5 Comments

Actually,my case is different. This one is for change password option. I already have current password in object $pass. I want to validate this $pass against textbox input current_password to proceed to create a new password for the user.
let me know if here is what you want. so user input current password in current_password text field , then user click Change password button which will create new password for the user ( show create new password form ) ? am i right ?
Yes..I already have old password in $pass variable. I want to validate this $pass against current_password value while clicking change password button.
Thank you for the information. I was not aware of it. It helped me to solve it.
Is there any validator with similar functionality?
1

If you have password stored in $pass already just inject $pass in the request and use its field instead for e.g.

$request->request->add(['password_old' => $pass]);

Then, you can validate it like

$rules = array('password_current' => "required|same:password_old");

Comments

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.