Essentials / Validation Response

Docs Essentials

Validation Response

By default, when validation fails, Laravel redirects "back" to the page the request came from. This isn't usually a problem. In fact, it's the expected behavior, since that's usually the page that renders the form which triggered the request.

However, this becomes a problem with Turbo Frames, since a form might get injected into a page that doesn't initially render it. After a failed validation, Laravel would redirect "back" to the page where the form was injected and, since that page doesn't render the form initially, the user would see the form disappear.

In other words, we can't redirect "back" to display the form again with the error messages, because that page might not render the form. Instead, Turbo expects us to return the form and validation messages with a non-200 HTTP status code right away after a validation failure.

Turbo Laravel automatically prepends a TurboMiddleware on the web route group. The middleware intercepts the response when it detects that Laravel is responding to a ValidationException. Instead of sending the "redirect back" response, it tries to guess where the form for that request usually renders, sends an internal request back to the app to render the form, and then changes the status code to 422 instead of 200.

To guess where the form is located, it relies on the resource route naming convention. For any route name ending in .store, it will guess that the form is located in a similar route ending with .create for the same resource. Similarly, for any route ending with .update, it will guess the form is located at a route ending with .edit. Additionally, for any route ending with .destroy, it will guess the form is located at a route ending with .delete (this is the only one that isn't part of Laravel's default conventions).

For this internal request, the middleware will pass along any route parameters of the current route, as well as any query string that was passed.

Here are some examples:

  • posts.comments.store will guess the form is at the posts.comments.create route with the {post} route param.
  • comments.store will guess the form is at the comments.create route with no route params.
  • comments.update will guess the form is at the comments.edit route with the {comment} param.

If a guessed route name doesn't exist (which will always happen if you don't use the route resource convention), the middleware will not change the default handling of validation errors, so the regular "redirect back" behavior applies.

When you're not using the resource route naming convention, you may override the redirect behavior by catching the ValidationException and re-throwing it with the location where the form renders, using the redirectTo method. If the exception has a redirect location set, the middleware will respect it and make a GET request to that location instead of guessing:

php
public function store()
{
  try {
     request()->validate(['name' => 'required']);
  } catch (\Illuminate\Validation\ValidationException $exception) {
    throw $exception->redirectTo(url('/somewhere'));
  }
}

If you want to register exceptions to this route guessing behavior, add the URIs to the redirect_guessing_exceptions key in the config/turbo-laravel.php config file:

php
return [
    // ...
    'redirect_guessing_exceptions' => [
        '/some-page',
    ],
];

The Turbo HTTP Middleware

Turbo Laravel ships with a middleware which applies some conventions on your redirects, like the one for how failed validations are handled automatically by Laravel as described before. Read more about this in the Conventions section of the documentation.

The middleware is automatically prepended to your web route group middleware stack. If you want to add the middleware to other groups, make sure it's at the top of the middleware stack:

php
\HotwiredLaravel\TurboLaravel\Http\Middleware\TurboMiddleware::class,

Like so:

php

namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middlewareGroups = [ 'web' => [ \HotwiredLaravel\TurboLaravel\Http\Middleware\TurboMiddleware::class, // other middleware... ], ]; }