How to validate Route Parameters in Laravel 5?

后端 未结 4 1800
無奈伤痛
無奈伤痛 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',
        ];
    }
    
    0 讨论(0)
  • 2021-02-12 18:56
    public function listTurns($doctor_id, $limit, $offset){
            $r = [
                'doctor_id' => $doctor_id,
                'limit' => $limit,
                'offset' => $offset,
            ];
    
            $validator = Validator::make($r, [
                'doctor_id' => 'required|numeric|min:1|exists:doctors,id',
                'limit' => 'required|numeric|min:1',
                'offset' => 'required|numeric|min:0',
            ]);
    }
    
    0 讨论(0)
  • 2021-02-12 19:04

    Supposue the following route:

    Route::get('profile/{id}', 'ProfileController@show');
    

    You can still validate id parameter as L4 way:

    public function show(){
        $validator = \Validator::make(
            \Input::all(),
            [
                 'id' => ['required', 'numeric']
            ]
        );
    
        // run validator here
    }
    

    If you need to validate concrete data, take a look the following example:

    public function getUsername(Request $request, $username)
    {
        $validator = \Validator::make(
            [
                 'username' => $username
            ],
            [
                 'username' => ['required']
            ]
        );
    
        // run the validator here
    }
    

    L5 let you do in two other ways. The first one, using a generic Request class injected in the controller:

    public function show(Request $request){
        $this->validate($request, [
            'id' => ['required', 'numeric']
        ]);
    
        // do stuff here, everything was ok
    }
    

    In L5 you are allowed to call validate() functions that receive the request and the rules to run over it. This functions is in charge of run rules, if some rule fails, then the user is redirected to previous request

    Finally, as second option, you can use Form request validation. Remember, every GET and POST value can be accessed via Request class

    0 讨论(0)
  • 2021-02-12 19:08

    If you plan to do this directly in your controller method you can do something like:

            public function getUser(Request $request)
        {
            $request->merge(['id' => $request->route('id')]);
            $request->validate([
                'id' => [
                    'required',
                    'exists:users,id'
                ]
            ]);
        }
    

    To do this in a custom FormRequest class, add the following:

        protected function prepareForValidation() 
        {
            $this->merge(['id' => $this->route('id')]);
        }
    

    And in your rules method:

        public function rules()
        {
            return [
                'id' => [
                    'required',
                    'exists:users,id'
                ]
            ];
        }
    
    0 讨论(0)
提交回复
热议问题