检查旧密码和新密码的规则

时间:2015-02-03 11:33:39

标签: php laravel laravel-4 laravel-validation

我正在使用确认密码检查旧密码和新密码。

在这里,我想检查OldPassword和New Password是否应该相同。

我该怎么做?

这是我的规则:

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

这是我的验证控制器代码:

$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');
        }

注意:我正在使用模型验证规则..我怎么能在模型中做到这一点??

1 个答案:

答案 0 :(得分:3)

只需使用different验证规则 - 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',
    );

另外 - 为什么你将密码限制为10个字符?这很愚蠢 - 根本没有理由限制它。您所做的只是降低应用程序的安全性。