laravel - 如何在 Laravel 中验证 Patch/Put 请求

标签 laravel laravel-5

如何在 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/

相关文章:

php - Eloquent laravel : How to get a row count from a ->get()

php - 如何将动态变量发送到部分页面 "sidebar"?

php - Laravel 5 中的自定义验证器

php - Laravel5 类 'Laravel\Socialite\SocialiteServiceProvider' 未找到

composer-php - laravel 5 自动加载不加载模型

mysql - Laravel 连接查询返回空列

php - Laravel 5.2 如何通过 Laravel Eloquent 模型连接三个表

php - 如何在 laravel 5.2 中检索行中的常用单词

javascript - 如何使用 Javascript 调用路由并销毁数据库中的数据?

mysql - Laravel - 更新数据库中的动态表单字段