【问题标题】:Custom validation help for laravel - check field against DB fieldlaravel 的自定义验证帮助 - 对照 DB 字段检查字段
【发布时间】:2014-12-07 11:02:06
【问题描述】:

想办法让我对照数据库的密码检查输入密码。

对于数据库值,我使用的是 Auth 功能。到目前为止,我有:

$currentPassword = Auth::user()->password;
$passwordInput = Input::get('password-old');

if (isset($passwordPassword) && !Hash::check($passwordInput, $currentPassword))
{
return Redirect::to('/updatepassword')->with('incorrectPassword', '1');
}

然后我正在检查重定向发送的不正确密码。这有效,但它很难看。我想创建一个自定义验证,我可以在其中根据给定值检查字段。

所以:

'password' = > 'checkMatch:$checkValue'

还是什么?

对此有任何帮助吗?我已经研究了如何实现自定义验证规则,但如果没有一个工作示例,我就无法理解。

【问题讨论】:

    标签: php validation laravel laravel-4


    【解决方案1】:

    Validator::extend('checkMatch', function($attribute, $value, $parameters)
    {
       if (count($parameters) < 1)
          throw new InvalidArgumentException("Validation rule checkMatch requires at least 1 parameters.");
    
       return Hash::check($value, $parameters[0]);
    });
    

    【讨论】:

    • 谢谢,这个怎么用?如 'password' => 'required|checkMatch:?'
    • 是的,'password' =&gt; 'required|checkMath:yourPasswordHere'
    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2013-01-06
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多