How to validate Route Parameters in Laravel 5?

后端 未结 4 1801
無奈伤痛
無奈伤痛 2021-02-12 18:23

As you know Laravel 5 changes the way you call the validator, the old way is calling the validator facade, but now there is the ValidatesRequests

4条回答
  •  旧巷少年郎
    2021-02-12 18:56

    Manix's answer wasn't working for me, I was having the same issues as Iliyass. The issue is route parameters aren't automatically available to the FormRequest. I ended up overriding the all() function in my particular FormRequest Class:

    public function all()
    {
        // Include the next line if you need form data, too.
        $request = Input::all();
        $request['username'] = $this->route('username');
        return $request
    }
    

    Then you can code rules as normal:

    public function rules()
    {
        return [
            'username' => 'required',
        ];
    }
    

提交回复
热议问题