From ad99714882426c0524a8c203910d2499f49855c8 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 21 Jul 2018 17:50:48 +0800 Subject: [PATCH] Fix handling validation errors --- app/Exceptions/Handler.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index a73e1384..a8dec512 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -62,11 +62,15 @@ class Handler extends ExceptionHandler if ($e instanceof ValidationException) { // Quick fix for returning 422 // @see https://prinzeugen.net/custom-responses-of-laravel-validations/ - $e->status = 200; - return response()->json([ - 'errno' => 1, - 'msg' => array_flatten($e->errors())[0] - ], 200); + if ($request->expectsJson()) { + return response()->json([ + 'errno' => 1, + 'msg' => $e->validator->errors()->first() + ]); + } else { + $request->session()->flash('errors', $e->validator->errors()); + return redirect()->back(); + } } foreach ($this->dontReport as $type) {