chimit's avatar

Getting validation errors from the Validation Exception

I want to catch and handle validation errors in my own way, but I don't know how to get validation errors array from the ValidationException:

    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email|unique:users'
    ]);

Lumen documentation tells us:

Should validation fail, the $this->validate helper will throw Illuminate\Validation\ValidationException with embedded JSON response that includes all relevant error messages.

So what is the correct way to get validation errors array? Currently, I'm using $e->getResponse()->getOriginalContent(), but I'm not sure it's the best way to do this.

0 likes
2 replies
alsofronie's avatar
Level 7

The correct way will be to access to MessageBag type object of the public property $validator in ValidationException. But I do it like you, after all... it's an already JSON formatted string for you to grab.

1 like
Snapey's avatar

At least in Laravel, you can manually validate. Not sure about Lumen;

$validator = Validator::make($request,$rules);

$if($validator->fails()){

    // handle it

}

//sweet, no errors
 
1 like

Please or to participate in this conversation.