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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28297886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:49:01  来源:igfitidea点击:

Rule for Checking Old Password and New Password

phplaravellaravel-4laravel-validation

提问by AngularAngularAngular

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.

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

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 ??

注意:我正在使用模型作为验证规则。我如何在模型中做到这一点?

采纳答案by Laurence

Just use the differentvalidation rule - as described in the Laravel docs

只需使用different验证规则 -如 Laravel 文档中所述

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

Also - whyare 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.

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