>我在这里引用了Laravel 4.2验证规则 - 当前密码必须与数据库值匹配
这是我的密码确认规则:
public static $ruleschangepwd = array(
'OldPassword' => array( 'required'), // need to have my rule here
'NewPassword' => 'required|confirmed|alphaNum|min:5|max:10'
);
但我在模型中有我的规则
正如我在问题中看到下面给出的自定义规则
Validator::extend('hashmatch', function($attribute, $value, $parameters)
{
return Hash::check($value, Auth::user()->$parameters[0]);
});
$messages = array(
'hashmatch' => 'Your current password must match your account password.'
);
$rules = array(
'current_password' => 'required|hashmatch:password',
'password' => 'required|confirmed|min:4|different:current_password'
);
有没有可能有这样的规则?
'OldPassword' => array( 'required', 'match:Auth::user()->password')
像这样或上面给出的任何简单的自定义规则?
注意:由于我在模型中执行此操作,因此无法在模型中实现上述自定义规则。(或者如果可以,我如何在模型中做到这一点)
更新:
我可以使用这样的东西吗
'OldPassword' => array( 'required' , 'same|Auth::user()->password'),
但我应该
Hash::check('plain text password', 'bcrypt hash')
您必须使用自定义规则扩展验证器。但是,如果您在模型中有规则,则应该没有问题。您可以将验证器扩展到任何地方,规则将全局可用。
我建议您在项目中添加一个新文件app/validators.php
然后在app/start/global.php
底部添加此行
require app_path().'/validators.php';
现在validators.php
里面定义验证规则
Validator::extend('match_auth_user_password', function($attribute, $value, $parameters){
return Hash::check($value, Auth::user()->password);
}
(我稍微更改了名称以更具描述性。你显然可以使用任何你喜欢的名字)
之后,将match_auth_user_password
添加到您的规则中:
public static $ruleschangepwd = array(
'OldPassword' => 'required|match_auth_user_password',
'NewPassword' => 'required|confirmed|alphaNum|min:5|max:10'
);