Search code examples
phplaravelformsauthenticationvalidation

How to Validate Current Password Against User Input Using Laravel Validator?


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?


Solution

  • 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