Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel same validator

Tags:

php

laravel

My case is for change password option. I already have current password in object $pass. I want to validate this $pass against textbox form input current_password to proceed to create a new password for the user. How to validate with same validator. Sorry I'm new to laravel.

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

doesn't work.

like image 825
Nivin V Joseph Avatar asked Jan 09 '23 01:01

Nivin V Joseph


2 Answers

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

like image 185
Dark Cyber Avatar answered Jan 11 '23 13:01

Dark Cyber


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");
like image 28
Shan Avatar answered Jan 11 '23 15:01

Shan