laravel :在验证之前清理请求数据

Posted

技术标签:

【中文标题】laravel :在验证之前清理请求数据【英文标题】:laravel : sanitize request data before validation 【发布时间】:2016-06-04 10:21:17 【问题描述】:

有一个UpdateUserRequest 表单请求根据rules mathod 中定义的规则验证字段值。默认情况下它具有rules() 和authorize() 方法。我想阻止验证和更新空字段(例如密码)。

在规则中使用sometimes 没有用,因为 POST 请求中会出现 html 输入,即使它们是空的。

array:6 [▼
 "_method" => "PATCH"
 "_token" => "Cz79rRez2f6MG0tTU17nVwXD0X1lNGH1hA7OORjm"
 "name" => "john"
 "email" => "mymail@gmail.com"
 "password" => ""
 "password_confirmation" => ""

]

所以我应该在规则中使用 sometimes 之前删除 POST 请求的空键。 问题是:清除请求数组的最佳位置在哪里? 是否有任何 laravel 内置方法来管理这种情况?

附注:解决方案: @Bogdon 解决方案仍然有效且有效,但从 here 采用了另一个简单、漂亮、整洁的解决方案:只需在表单请求中覆盖 all() 方法

 class RegistrationRequest extends Request
  

...

public function all()

    $attributes = parent::all();

    if(isset($attributes['password']) && empty($attributes['password'])) 
        
            unset($attributes['password']);
        
    $this->replace($attributes);

    return parent::all();



...


【问题讨论】:

【参考方案1】:

要完成这项工作,您需要修改 App\Http\Requests\Request 类的内容以允许对输入进行清理(类代码取自 this Laracasts post):

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

abstract class Request extends FormRequest

    /**
     * Validate the input.
     *
     * @param  \Illuminate\Validation\Factory  $factory
     * @return \Illuminate\Validation\Validator
     */
    public function validator($factory)
    
        return $factory->make(
            $this->sanitizeInput(), $this->container->call([$this, 'rules']), $this->messages()
        );
    

    /**
     * Sanitize the input.
     *
     * @return array
     */
    protected function sanitizeInput()
    
        if (method_exists($this, 'sanitize'))
        
            return $this->container->call([$this, 'sanitize']);
        

        return $this->all();
    

之后,您只需在UpdateUserRequest 类中编写添加sanitize 方法,当password 字段为空时从输入中删除:

public function sanitize()

    if (empty($this->get('password'))) 
        // Get all input
        $input = $this->all();
        // Remove the password field
        unset($input['password']);
        // Replace the input with the modified one
        $this->replace($input);
    

    return $this->all();

现在对密码字段使用sometimes 规则将起作用:

public function rules()

    return [
        // Other rules go here
        'password' => 'sometimes|required|confirmed'
    ];

【讨论】:

thanks.but 如果用户打算更改他的密码怎么办?当它的字段为空时,密码应该是完整的,当密码字段被填充时应该更新。顺便说一下,我有两个申请表,一个用于创建新用户,一个用于更新用户。我也认为没有必要在规则中添加password_confirmation,只需将confirmed添加到password即可。 我的错,你是对的,我试图说明密码确认的问题,但我错误地添加了 confirmed 规则:)。除此之外,如果用户打算更新密码,请不要包含update 字段(或者如果包含,请确保该值不是1)。简而言之:如果请求数据包含update=1 字段,那么密码是可选的,否则是必需的 您专注于规则,而我正在寻找一个地方(方法)来清除请求日期,然后再验证规则并将该数据传递给控制器​​中的更新方法。事实上,我正在考虑register 优先于boot 的服务提供商中的registerboot 方法。 哦,对不起,我被自己的想法卡住了,我现在明白了。我已经用解决方案更新了我的答案。【参考方案2】:

我不确定清除字段的最佳方式,但这是我目前在我的系统上处理用户更新的方式。

我根据传递过来的$id找到用户,然后更新相应的记录。我假设nameemail 永远不会为空,只有密码可以为空 - 所以我们可以将nameemail 字段设置为传入的值,然后使用@ 987654326@ 语句检查password 字段是否为空并相应更新。

我使用的是类似的东西:

public function update($id)
    
        $user = User::find($id);

        $user->name   = Input::get('name');
        $user->email      = Input::get('email');
        if (Input::get('password') != "")
        
        $user->password   = Hash::make(Input::get('password'));
        

        $user->save();
    

【讨论】:

以上是关于laravel :在验证之前清理请求数据的主要内容,如果未能解决你的问题,请参考以下文章

Larave中CSRF攻击

使用 laravel 验证检查唯一的日期/时间

laravel 5.4在请求中验证之前修改数据[关闭]

Laravel 请求在验证失败时不返回修改后的请求

是否可以使用 API 令牌在 laravel 中进行用户模拟?

laraver框架学习------工厂模型填充测试数据