1

I have a simple controller, Laravel 5.5, with some basic validation but when I enter invalid data, I submit the form and get a "ValidationException: The given data was invalid".

The framework is supposed to redirect me to the form with error messages, but I end up on the big red exception page.

Why would this be?

/**
 * Store blog comment
 */
public function store(Request $request)
{
    try {
        $blogPost = BlogPost::where('id', '=', $request->blog_post_id)->get()->first();
        if (!$blogPost) {
            abort(404);
        }

        $validatedData = $this->validate($request, [
            'blog_post_id' => 'required|numeric',
            'blog_comment' => 'required|min:3',
            'blog_comment_name' => 'required|min:3',
            'blog_comment_company' => 'nullable'
        ]);

Here is the controller that is sending me back to the exception page.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
  • 4
    For us to help you, post your form data or json data on the request and the validation data the exception has. And if the exception is not redirect etc, you need to check what happens in your handler. Meanwhile your try should catch that, there needs some details more before we can help :) – mrhn Mar 19 '20 at 22:29
  • Searching for your error msg turns up some clues, namely that that exception and msg is what Laravel's validation does by default, but it is automatically caught and handled to work as we expect. [This question](https://stackoverflow.com/questions/46257191/failed-validation-returns-default-error-message-even-though-custom-messages-supp) further suggests it is mostly likely your own exception handling in that question interfering with Laravel's standard behaviour. Move your validation out of your try/catch. – Don't Panic Mar 20 '20 at 00:23

1 Answers1

1

The issue I was facing was because of a custom Exception Handler not doing what it was supposed to do.

Laravel by default does not report Validation exceptions as Exceptions. If ValidationException is NOT in the dontReport list, Validation errors cause the app to throw an exception.

/**
 * A list of the exception types that are not reported.
 *
 * @var array
 */
protected $dontReport = [
    AuthorizationException::class,
    HttpException::class,
    ModelNotFoundException::class,
    ValidationException::class,
];
Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116