Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arrrssssss's avatar

Inertia 2 WhenVisible on Form Server-side validation, failed!

Hi Community,

I used inertia 2 Inertia::optional to lazy load data and in my vue component when I use WhenVisible tag, in the first load everything seems ok, but within the WhenVisible tag code, I have a Inertia form that post data to a route, This route has a validation, and when ever the validation fails, the lazy loaded data lost in page and everything goes blank!

Is one experienced same issue? and is there any fix for this write now ?

Regards

1 like
1 reply
LaryAI's avatar
Level 58

Certainly! This is a common issue when using Inertia's lazy loading (Inertia::lazy or Inertia::optional) in combination with server-side validation errors. Here’s what’s happening and how you can fix it:

Problem Explanation

When you use Inertia::lazy or Inertia::optional to defer loading of certain props, those props are only loaded when explicitly requested (e.g., via a client-side visit with preserveState). However, when you submit a form and validation fails, Laravel typically redirects back with errors, but does not re-trigger the lazy-loaded data unless you explicitly request it again.

As a result, after a failed validation, your lazy-loaded data is missing, causing your component (inside <WhenVisible>) to go blank.


Solution

You need to ensure that the lazy-loaded data is always included when returning validation errors.

Option 1: Eagerly Load Data on Validation Failure

Instead of returning the lazy prop, return the actual data if validation fails.

Example Controller:

Option 2: Always Load Data If Validation Fails

A more DRY approach is to check if there are errors and load the data accordingly:

public function create()
{
    $lazyData = request()->old() || session('errors')
        ? $this->getLazyData()
        : Inertia::lazy(fn () => $this->getLazyData());

    return Inertia::render('YourComponent', [
        'lazyData' => $lazyData,
    ]);
}

Summary

  • Lazy props are not included on validation error redirects.
  • On validation failure, return the actual data instead of a lazy prop.
  • This ensures your component always has the data it needs, even after a failed form submission.

References:

Let me know if you need a more specific code example tailored to your setup!

Please or to participate in this conversation.