How to validate a Patch/Put request in Laravel

- 1 answer

Ad

How do I validate a Patch/Put request in Laravel

According to Laravel documentation http://laravel.com/docs/5.1/controllers, put/patch requests are handled by update action of a resource controller

Verb         Path           Action  Route Name
PUT/PATCH   /photo/{photo}  update  photo.update

Since a patch request should update partial resource and put request updates whole resource, how then should my FormRequest validation rules look like:

Should I be doing something like this:

public function rules()
{

    $rules = [];

    if($this->has('name')) $rules['name'] = 'required';
    if($this->has('email')) $rules['email'] = 'required|email';

    return $rules;
}

Counting on your professional answers.

Ad

Answer

Ad

Try to override default getValidatorInstance method of your FormRequest class

I don't try it, but I think it should work :) Hope the main idea is clear

 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;
    }
Ad
source: stackoverflow.com
Ad