Closed NicolasSacC closed 10 months ago
I found that it was because url()->previous()
after the validation fails returns the homepage for some reason, but I have no idea why...
@NicolasSacC, see: https://inertiajs.com/redirects
This happens cause when using Inertia, previous URL is not stored due to AJAX requests used by Inertia to facilitate the SPA functionality. You can see why it is the case here. It can be solved if Laravel can consider Inertia requests as valid request to store as current URL. In that case, changing it to the following works.
protected function storeCurrentUrl(Request $request, $session)
{
if ($request->isMethod('GET') &&
$request->route() instanceof Route &&
! ($request->ajax() && ! $request->inertia()) &&
! $request->prefetch() &&
! $request->isPrecognitive()) {
$session->setPreviousUrl($request->fullUrl());
}
}
For now, you can create the FormRequest instead of defining the validation logic in controller and inside the FormRequest you can assign $redirectRoute property to route where you want to redirect in case of failed validation.
You can also create your own StartInertiaSession middleware and extend the original Middleware provided by Laravel. Just override the StoreCurrentUrl method as shown above and change the \Illuminate\Session\Middleware\StartSession::class, to \App\Http\Middleware\StartInertiaSession::class in $middlewareGroups array of app\Http\Kernel.php and it should work too. You can see the modified middleware here.
Although, I still think Inertia should update the session key for previous URL internally. I think @reinink should look at this because it will redirect to the page where you navigated first using normal request in case of failed validation. That causes very abrupt behavior and create a confusion for devs using the Inertia.
I found that it was because
url()->previous()
after the validation fails returns the homepage for some reason, but I have no idea why...
Glad you got it figured out! 👍
I had this problem today, I found it and solved it, it's an inertia import problem
First step : You will uninstall: @inertiajs/vue3
npm uninstall @inertiajs/vue3 or yarn remove @inertiajs/vue3
Second step : You will install : @inertiajs/inertia-vue3
npm install @inertiajs/inertia-vue3 or yarn add @inertiajs/inertia-vue3
Third step : go to your app.js file in ressources/js
Replace
import { createInertiaApp } from '@inertiajs/vue3' by import { createInertiaApp } from '@inertiajs/inertia-vue3'
Last step : Replace everywhere
import { createInertiaApp } from '@inertiajs/vue3' by import { createInertiaApp } from '@inertiajs/inertia-vue3'
Delete the nodes_modules folder and do an : npm install or yarn install
Hope this helps you if you encounter this problem 😉
Hi!
I have this weird issue: Inertia constantly redirects to / after a failed validation.
If a form validation fails, Inertia returns a response with the parameter url as "/".
However, there is one component where it doesn't happen. "Pages/Dashboard"
Any other component has that issue.
See a comparison here (first is ok, second redirects to /):