I want to create form with 3 field (old_password, new_password, confirm_password) with laravel 5.
View
old password :
{!! Form::password(
This is change password form
Create rules
public function admin_credential_rules(array $data)
{
$messages = [
'current-password.required' => 'Please enter current password',
'password.required' => 'Please enter password',
];
$validator = Validator::make($data, [
'current-password' => 'required',
'password' => 'required|same:password',
'password_confirmation' => 'required|same:password',
], $messages);
return $validator;
}
User controller method to changes password
use Validator;
public function postCredentials(Request $request)
{
if(Auth::Check())
{
$request_data = $request->All();
$validator = $this->admin_credential_rules($request_data);
if($validator->fails())
{
return response()->json(array('error' => $validator->getMessageBag()->toArray()), 400);
}
else
{
$current_password = Auth::User()->password;
if(Hash::check($request_data['current-password'], $current_password))
{
$user_id = Auth::User()->id;
$obj_user = User::find($user_id);
$obj_user->password = Hash::make($request_data['password']);
$obj_user->save();
return "ok";
}
else
{
$error = array('current-password' => 'Please enter correct current password');
return response()->json(array('error' => $error), 400);
}
}
}
else
{
return redirect()->to('/');
}
}