laravel-validator icon indicating copy to clipboard operation
laravel-validator copied to clipboard

Validate certain fields PATCH

Open ghost opened this issue 9 years ago • 0 comments

Hi,

I am trying to modify an entity by using the PATCH method. The entity has 3 fields: 'firstname', 'lastname' and 'address', so I am sending only one of those parameters to the update method of the repository. However it is validating all of them.

This is my validator

class UserValidator extends LaravelValidator
{
    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'firstname' => 'required',
            'lastname' => 'required',
            'address' => 'required'
        ],
        ValidatorInterface::RULE_UPDATE => [
            'firstname' => 'required',
            'lastname' => 'required',
            'address' => 'required'
        ],
   ];  
}

This is my controller:

public function update(Request $request, $id)
{
       $this->repository->update($request->all(), $id);
        return $this->response->created();
}  

The $request->all is a single field. Additionally this is my repository:

use Prettus\Repository\Eloquent\BaseRepository;
class UserRepositoryEloquent extends BaseRepository
{
    # do nothing because it extends the update of the BaseRpository
}

However by sending a single field to update, the others fields are being validated and as a consequence I receive an validation error. The only field that should be validated is the one send in the attributes array. I would like to know how could I use the PATCH then?

Thanks!

ghost avatar Oct 04 '16 20:10 ghost