2

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.

2
  • 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 :) Commented Mar 19, 2020 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 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. Commented Mar 20, 2020 at 0:23

1 Answer 1

2

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,
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.