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, the user would see the form disappear.

Turbo Laravel's middleware fixes this. When a form submitted from inside a Turbo Frame fails validation, it redirects to the page that renders the form instead. Turbo follows the redirect inside the frame, and the form shows up again with its errors and the user's input, through laravel's error() and old() template functions.

To find the form's page, it relies on the resource route convention:

  • a failure in a store handler, such as posts::store on /posts, redirects to create, so /posts/create;
  • a failure in an update handler, such as posts::update on /posts/{post}, redirects to edit, so /posts/{post}/edit.

The redirect keeps the current route's parameters, so a nested comments::store on /posts/{post}/comments redirects to /posts/{post}/comments/create. If the resource has no such route, the usual redirect back applies.

Other Routes

When a form isn't handled by a resource's store or update, map its handler to the form's in src/app.rs, by replacing the middleware the provider registered:

rust
use laravel::Application;

Application::configure()
    .with_middleware(|middleware, _| {
        middleware.web(|web| {
            web.remove::<turbo::Middleware>();
            web.prepend(turbo::Middleware::new().redirect_frame_failures(posts::publish, posts::review));
        });
    });

A failed validation in a frame on posts::publish's route now redirects to posts::review's, with the same route parameters.

To turn the frame redirects off, use turbo::Middleware::new().without_frame_redirects() instead, or set TURBO_FRAME_REDIRECTS=false in your .env.

The Turbo Middleware

Turbo Laravel ships with a middleware that applies a few conventions to your responses. The provider prepends it to the web middleware group, so you don't need to register it. Besides the frame redirects above, it:

  • turns a 302 Found after a Turbo form submission into a 303 See Other, so Turbo follows the redirect with a GET, as the Turbo Handbook requires;
  • keeps the request's X-Turbo-Request-Id, so refresh broadcasts don't refresh the page that made the change;
  • tells templates whether the request comes from a Hotwire Native app, for is_native().

The middleware must come before the session starts, so it sees the errors a failed validation flashes. That's why the example above uses prepend. If you add it to another group, prepend it there too.