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

Double Validation: in controller and repository.

Open akozyr opened this issue 7 years ago • 2 comments

We have possibility to run a validation in two different way: run a validator directly in the controller and validate request data in the repository during create/update. I have found that when we use the validator in controller we will have double validation on the whole. The discribed situation:

class PostsController
{
    ...

    public function store(...)
    {
        try {
            // the first validation executing
            $this->validator->with($request->all())->passesOrFail();

            $someService->update($request->all(), $id);
        } catch (ValidatorException $e) {
            
        }
    }
}
class SomeService
{
    public function update(...)
    {
        // the second validation executing in the parent method of BaseRepository
        $this->repository->update($data, $id);
    }
}

Question: what is the best way to avoid double validation leaving it only in the controller?

akozyr avatar May 15 '18 14:05 akozyr

Will it be correct if we just don't specify validator method in the repository?

akozyr avatar May 15 '18 14:05 akozyr

Indeed, it's sucks! Actually I replace the validator on Controller and just keep it on Repository

celorodovalho avatar Nov 16 '20 19:11 celorodovalho