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

idlerus's avatar

Request validator removes all flashes from session

I have problem with session flashes. When i return Session::flash('message', 'test'); The message gets stored into session without problem.

Eveytime I do something like

	    Session::flash('message', 'test');
        $request->validate([
            'email' => 'required'
        ]);

The flash data gets lost and $errors->all() is empty.

0 likes
11 replies
idlerus's avatar
$validation = Validator::make($request, [
            'email' => 'required'
        ]);

Does the same problem.

Also tried request class, it has the same problem.

Nothing returns $errors->all() is empty, flash messages get lost.

idlerus's avatar

@migsav sure,

The form works, the data gets passed even when i use ->withErrors('message') at the end without validator, it works.

<body class="antialiased">
{{ var_export($errors) }}
{{ var_export(Session::all()) }}
<ul>
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
</ul>
    <div class="login-form">
        <h4>Přihlášení</h4>
        <form class="form" method="POST">
            @csrf
            <label>
                <p>Login</p>
                <input type="text" name="email">
            </label>
            <label>
                <p>Heslo</p>
                <input type="password" name="pass">
            </label>
            <input type="submit" value="Log in">
        </form>
    </div>
</body>

web.php routes

Route::get('/a/lgn',  [LoginController::class, 'index'])->name('admin.login');
Route::post('/a/lgn', [LoginController::class, 'login'])->name('admin.login.in');

whole controller method

 public function login(Request $request): Application|Factory|View|RedirectResponse
    {
        Session::flash('message', 'test');
        $request->validate([
            'email' => 'required'
        ]);

        if(Auth::attempt(['email' => $request->post('email'), 'password' => $request->post('pass')]))
        {
            return redirect('a/');
        }

        return view('admin.login');
    }
migsAV's avatar

@idlerus you don't have the action in your form

<form class="form" method="POST" action="{{ route('admin.login.in') }}">
            @csrf
		// inputs
        </form>
idlerus's avatar

Added the action, the session flash appeared, but the validation errors are not there

idlerus's avatar

Found out that even {{ old('email') }} is empty. It seems that validation just flushes all form data no matter what.

migsAV's avatar

@idlerus the session flash will appear if your validation passes.

Add a password validation and see if {{ old('email') }} appears

$request->validate([
            'email' => 'required',
			'pass' => 'required'
        ]);
idlerus's avatar

@migsAV Tried, if the validation passes, the data gets to work fine, if the validation doesn't pass. All data from old and $errors are empty

migsAV's avatar

@idlerus that is very strange, it is working on my side and the only difference is, I removed Application|Factory|View|RedirectResponse from your method

 public function login(Request $request)
    {
       // your code
    }

See if this works on your side

Sinnbeck's avatar

@idlerus Sounds like your should perhaps test it out on a new project. If it works there, you know it is caused by some other code in your project :)

Please or to participate in this conversation.