【问题标题】:After validation hook in validation request验证请求中的验证钩子后
【发布时间】:2018-05-31 14:55:29
【问题描述】:

我可以在验证钩子 (documentation) 后使用 php artisan make:request 附加到我的自定义请求吗?

【问题讨论】:

    标签: php laravel laravel-5 laravel-validation laravel-request


    【解决方案1】:

    您可以像这样在自定义请求类中覆盖getValidatorInstance() 方法:

    protected function getValidatorInstance()
    {
       $validator = parent::getValidatorInstance();
    
       // here you can apply hook (example hook taken from documentation):
    
        $validator->after(function ($validator) {
           if ($this->somethingElseIsInvalid()) {
              $validator->errors()->add('field', 'Something is wrong with this field!');
           }
       });
    
       return $validator;
    }   
    

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 2016-01-16
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2017-11-21
      • 2020-01-13
      相关资源
      最近更新 更多