Search code examples
phplaravellaravel-4laravel-validation

Rule for Checking Old Password and New Password


I am checking for Old Password and New Password with Confirmation Password.

Here i want to check with whether OldPassword and New Password should not be same.

How can i do this ?

Here is my Rule :

   public static $rulespwd = array('OldPassword' => 'required|pwdvalidation',
        'NewPassword' => 'required|confirmed|min:1|max:10',
        'NewPassword_confirmation' => 'required',
        );

Here is my controller code for the validation :

$PasswordData = Input::all();
        Validator::extend('pwdvalidation', function($field, $value, $parameters)
        {
            return Hash::check($value, Auth::user()->password);
        });

        $messages = array('pwdvalidation' => 'The Old Password is Incorrect');

        $validator = Validator::make($PasswordData, User::$rulespwd, $messages);
        if ($validator->passes()) 
        {
            $user = User::find(Auth::user()->id);
            $user->password = Input::get('NewPassword');
            $user->save();
            return Redirect::to('changepassword')->with('Messages', 'The Password Information was Updated');
        }

Note : I am using model for validation rule.. How can i do this in model ??


Solution

  • Just use the different validation rule - as described in the Laravel docs

    public static $rulespwd = array('OldPassword' => 'required|pwdvalidation',
        'NewPassword' => 'required|confirmed|min:6|max:50|different:OldPassword',
        'NewPassword_confirmation' => 'required',
        );
    

    Also - why are you limiting a password to 10 chars? That is silly - there is no reason to limit it at all. All your are doing is reducing your application security.