如何在 Laravel 中验证 Patch/Put 请求
根据 Laravel 文档 http://laravel.com/docs/5.1/controllers , put/patch
请求由资源 Controller 的 update
操作处理
Verb Path Action Route Name
PUT/PATCH /photo/{photo} update photo.update
由于 patch
请求应该更新部分资源而 put
请求更新整个资源,那么我的 FormRequest
验证 rules
看起来像:
我应该做这样的事情吗:
public function rules()
{
$rules = [];
if($this->has('name')) $rules['name'] = 'required';
if($this->has('email')) $rules['email'] = 'required|email';
return $rules;
}
依靠您的专业答案。
最佳答案
尝试覆盖 FormRequest
类的默认 getValidatorInstance
方法
我没有尝试过,但我认为它应该有效:) 希望主要思想清楚
protected function getValidatorInstance()
{
$validator = \Validator::make($this->all(), $this->rules(), $this->messages(), $this->attributes());
$validator->sometimes('reason', 'required|max:500', function($input) {
return $input->games >= 100;
});
return $validator;
}
关于laravel - 如何在 Laravel 中验证 Patch/Put 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34418008/