【问题标题】:Laravel Custom validation rule with parametersLaravel 带参数的自定义验证规则
【发布时间】:2018-07-29 11:23:43
【问题描述】:

我已经在CustomRequest 中编写了这个函数规则来检查提供者中定义的 checkHackInputUser 规则:

其实我想检查路由中传入的值

例如:

 http://www.somedomain.com/user/{id} 

我对这个 $id 变量做了什么操作 使用我的 checkHackInputUser 规则

这里是自定义请求:

public function rules()
{

$request_id = $this->route('user');
$rules = [];

if($this->method() == "DELETE" || $this->method() == "GET" )
    $rules = [
        'role_list'             => 'required|checkHackInputUser:'.$request_id,
    ];

return $rules;
}

问题是,如果我删除required 角色,这个rule(checkHackInputUser) 将不起作用。

这里是 provider 中的 checkHackInputUser 验证函数:

public function boot()
{

    $this->app['validator']->extend('checkHackInputUser',function($attr,$value,$params){

            //Some validation

            return false or true;

});

}

【问题讨论】:

    标签: laravel laravel-5.3 laravel-validation


    【解决方案1】:

    您可以在使用sometimes 时输入conditionally validate

    在某些情况下,您可能希望仅当输入数组中存在该字段时才对该字段运行验证检查。要快速完成此操作,请将有时规则添加到您的规则列表中:

    $v = 验证器::make($data, [ '电子邮件' => '有时|需要|电子邮件', ]);

    【讨论】:

    • 我的问题是我的变量不在输入数组中
    • 它的路由 url 变量
    猜你喜欢
    • 2017-04-11
    • 2018-02-18
    • 2019-11-05
    • 2019-02-12
    • 2016-11-22
    • 2023-03-21
    • 2019-07-24
    • 2018-05-25
    • 2016-11-14
    相关资源
    最近更新 更多