首页 文章

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

提问于
浏览
5

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

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

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

]

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

P.S :解决方案:
@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();

}

...

}

2 回答

  • 0

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

    我找到了基于 $id 传递的用户,然后更新了相应的记录 . 我认为 nameemail 将永远是空的,这仅仅是可以为空密码 - 所以我们可以只设置 nameemail 字段中传递的值,然后使用 if 语句来检查 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();
        }
    
  • 3

    要完成这项工作,您需要修改 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 类中编写add 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'
        ];
    }
    

相关问题