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

georgecoffey's avatar

Validator only redirecting with Errors and Old sometimes

I have a form that gets submitted via POST to a request()->validate in a controller. However, if I submit the form with missing information, the "errors" and more frustratingly the old form values are only returned roughly 1 out of every 5 times. There does not seem to be a pattern to it, it will be blank 6 times in a row, then 2 or who knows. So far 9 is the most and once it showed the errors back to back. The validator always redirects on invalid input. There are pages this happens all the time, but there are also some forms where it never happens.

I have cut my validator back to only check one field with 1 required rule and that had no effect. I also am checking for the "session()->get('errors')" in the controller, so it's not a blade display issue.

I will keep trying to pour over what might be different with the forms it's happening on, but if anyone has had a similar problem or has ideas, let me know

0 likes
8 replies
krisi_gjika's avatar

are you maybe failing your validation in the first error? are you running in a server 500 error instead?

georgecoffey's avatar

@krisi_gjika My validation only has one rule now for testing, so it's failing on that one rule. Not getting any server errors.

martinbean's avatar

@georgecoffey

  • Where is this happening? Local environment or production environment?
  • What middleware is applied to the route?

We can’t really help without more context as we can’t see your screen or your code.

georgecoffey's avatar

@martinbean This was happening in my albeit limited production, and is happening locally as well. Originally it happened with custom middleware, but I moved the routes and it happens with the basic Jetstream Auth middleware, and without any middleware at all (although noticeably less often) I am adding another post now as I found something that seemingly fixed it.

georgecoffey's avatar

EDIT: Nevermind, the solution below did not change anything, it just stopped doing it for a few runs so I thought I had it fixed, but it started doing it again after making this post.

I found something that seems to fix it. In my original code, I had it setup like this:

Route:

Route::post('/manage/event/add', [TestController::class, 'save_new']);

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
	function save_new()
	{
		$this->save();
	}

	function save()
	{
		$postdata = request()->validate(['name' => ['required']]);
	}
}

I did this because both editing an existing "event" model and saving a new one used the same fields, so I had them share a save function. When I put the validate code in the "save_new()" function, the one called by the route, instead of the second "save()" function, it stopped happening. Does anyone know why this is? I don't understand why this makes a difference, and more importantly, why it still worked sometimes?

Additionally when I moved these routes outside of the jetstream middleware, it seemed to make it happen less.

georgecoffey's avatar

I finally sat down and slowly re-built the code piece by piece for this form. It never happened until I added a Livewire component. For each livewire component I added, it seemed to make it happen more and more often.

I'm now going to try to dig through what about these Livewire components might be causing this, but if anyone has any fresh incites, I'd love to hear them.

georgecoffey's avatar

Finally got to the bottom of this, it seems that this is a bug with Livewire and/or alpine. If you add the following livewire component to your form, it will cause this error almost all the time.

BadComponent.php

<?php

namespace App\Livewire;

use Livewire\Component;

class BadComponent extends Component
{
    public function testAction()
    {

    }
    public function render()
    {
        return view('livewire.bad-component');
    }
}

bad-component.blade.php

<div @click.outside="$wire.testAction()">
</div>
georgecoffey's avatar

the workaround is to use a setTimeout

<div @click.outside="setTimeout(function () {$wire.testAction();}, 100)">
</div>

Please or to participate in this conversation.