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

Invoking "errors" method causes fatal error

Open supercoffee opened this issue 9 years ago • 1 comments

After calling the "passes" method to validate data, calling the errors() method triggers a fatal error by attempting to invoke the all() method on an array.

My example code

        $data = $this->request->json();
        $this->validator->with->data($data)->passes(ValidatorInterface::RULE_CREATE);
        $this->validator->errors();

The error

PHP Fatal error:  Call to a member function all() on array in /var/www/my.app/vendor/prettus/laravel-validation/src/Prettus/Validator/AbstractValidator.php on line 92

supercoffee avatar Jul 03 '16 22:07 supercoffee

Hi @supercoffee,

use this

use Prettus\Validator\Contracts\ValidatorInterface;

   public function update(array $data, $id)
    {
        try {

            $this->validator->with($data)->passesOrFail(ValidatorInterface::RULE_UPDATE);
            return $this->repository->update($data,$id);

        } catch (ValidatorException $e){
            return Response::json([
                'error' => true,
                'message' => $e->getMessageBag()
            ],400);
        }
}

brunoneve avatar Jul 09 '16 01:07 brunoneve