A common experience on the web with a form is where we fill it out, we check it over, we hit submit and an error page is shown, we then hit the back button to find the form inputs empty. We flip the table and then come back to our senses, having to fill it out over again unsure if we’ll get the same error next time we click submit.

Out of the box Laravel does this as well in that all exceptions simply return the phrase “Whoops, looks like something went wrong”. Luckily it’s quite an easy thing to fix. What we are essentially going to do is change it so when a form is submitting, should an exception be thrown the browser will be told to return to the previous page with the form input as if our validation failed but also with an error message to display.

Getting started with a simple macro

You can potentially skip this part but I like to keep my code neat if I can so we’re going to add a macro function to our Request class. First of all I added a app/macros.php file to the project and then set it up as a file to be loaded through composer. You could also just add it to the AppServiceProvider and put it in the boot method should you prefer to have it that way.

In the macros.php file setup we’ll make a little shortcut for knowing if the request is a form submission.

What it does is simply check that the request is using the HTTP Post method and that the request isn’t an AJAX request. I’ve also added a check for if the application is in debug mode as it can be quite frustrating if we can’t view the error in Whoops during development. There’s obviously different things you could or might have to do for your application but this will do for now. Now with the macro created isFormSubmittedRequest() can be called as a shortcut for checking if the request is from a form.

Handle the Exception

Now we need to modify the exception handler in our Laravel project. This can be found in app/Exceptions/Handler.php where we will modify the render function.

What this code then does is in the event of an exception being thrown on a form submission we will redirect to the page we were on. We also put previous input into the session just like if the validation of a form failed but finally we also add an alert message for displaying somewhere on the page to let the user know that something failed.

Testing it out

We can now test this out in a fairly simplistic matter. First we’ll add some routes for our test form. One to show the form and another to simulate an exception being thrown when the form has been submitted.

We can now create a blade template ( resources/views/form.blade.php ) to show a very simple form with a single input, a single button and an area to show the alert message produced from a failure.

When we test this form out it will work as expected, filling the form with our previous input while still providing a message making it clear the form didn’t submit successfully.

If you’d like to see the project in full you can view it here on GitHub. Overall this is probably just the start, you should try to make your error messages more specific to what the end result of the form is but it’s a start in making your users lives easier when your system is failing them.