Skip to content

Commit

Permalink
clean up invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 7, 2017
1 parent 9c1eaae commit c264807
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,27 @@ protected function prepareException(Exception $e)
*/
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
if ($e->response) {
return $e->response;
}

if (method_exists($this, 'invalid')) {
return $this->invalid($request, $e);
}

if ($request->expectsJson()) {
return response()->json(
$this->formatValidationResponse($e), 422
);
}

return redirect()->back()->withInput($request->input())->withErrors(
$e->validator->errors()->getMessages(), $e->errorBag
);
return $e->response ? $e->response : $this->invalid($request, $e);
}

/**
* Format the validation JSON response payload.
* Convert a validation exception into a response.
*
* @param \Illuminate\Validation\ValidationException $e
* @return array
* @param \Illuminate\Http\Request $request
* @param Illuminate\Validation\ValidationException $exception
* @return \Illuminate\Http\Response
*/
protected function formatValidationResponse(ValidationException $e)
protected function invalid($request, ValidationException $exception)
{
return [
'message' => $e->getMessage(),
'errors' => $e->validator->errors()->getMessages(),
];
$message = $exception->getMessage();

$errors = $exception->validator->errors()->messages();

return $request->expectsJson()
? response()->json(['message' => $message, 'errors' => $errors], 422)
: redirect()->back()->withInput()->withErrors(
$errors, $exception->errorBag
);
}

/**
Expand Down

0 comments on commit c264807

Please sign in to comment.