williamxsp's avatar

Lumen + Service Layer - Display Error Message

Hi, Im trying to create a service layer and Id like to throw error messages from my service class.

If I do the validation inside the controller it works as expected:

 $this->validate($request, [
            'nomeArtistico' => 'required|max:255',
            'email' => 'required|unique:pessoas',
        ]);

Result:

{"email":["[email protected] j\u00e1 cadastrado"]}

But if I try to validate inside a service:

$validator =  Validator::make($request->all(), $this->validationRules)->validate();

It only throws an 500 error without the json message: (1/1) ValidationException The given data was invalid.

0 likes
2 replies
Talinon's avatar

I think your problem is that in the controller example, you can pass the entire $request object into the validate() method.

In your service, when you manually create the validator, I suspect you need to pass in array:

$validator =  Validator::make($request->all(), $this->validationRules)->validate();

Please or to participate in this conversation.