Validation failures always returns to the previous page in this case being the create view.
Form validation loses error bags and old input values on redirect
Hi there !
I have a view which contains a form
@extends('layouts.structure')
@section('content')
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="row" action="{{ route('lists.rules.create.store', ['list' => $list, 'condition' => $condition]) }}" method="POST">
@csrf
<!-- some form fields -->
</form>
@endsection
And for the form, I have a controller, defined like so :
<?php
namespace App\Http\Controllers\Lists\Rules;
use App\Http\Controllers\Controller;
use App\Models\ListUser;
use App\Models\ListUserRule;
use App\Models\ListUserRuleFlag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator as ValidatorFacade;
use Illuminate\Validation\Validator;
class StoreListUserRuleController extends Controller
{
private ListUser $list;
private function handleCreateCollectivities(): ListUserRule|Validator|null
{
$request = request();
$validator = ValidatorFacade::make($request->all(), [
'level' => 'required|exists:App\Models\Level,id',
'collectivities' => 'required|array|min:1',
'collectivities.*' => 'exists:App\Models\Collectivity,id',
'children' => 'required',
'neighboors' => 'required',
]);
if ($validator->fails()) {
return $validator;
}
$rule = ListUserRule::create();
$this->list->rules()->save($rule);
$rule->collectivities()->saveMany($request->input('collectivities'));
$rule->flags()->saveMany([
new ListUserRuleFlag(['name' => 'children', 'value' => $request->input('children')]),
new ListUserRuleFlag(['name' => 'neighboors', 'value' => $request->input('neighboors')])
]);
return $rule;
}
public function __invoke(Request $request, ListUser $list, string $condition)
{
$this->list = $list;
switch ($condition) {
case 'collectivites':
$ret = $this->handleCreateCollectivities();
if ($ret instanceof Validator) {
return Redirect::back()->withErrors($ret)->withInput();
}
return redirect()->route('lists.show', ['list' => $list]);
case 'mandats':
break;
case 'etiquettes':
break;
case 'nuances':
break;
case 'champs':
break;
}
}
}
However, upon form submission, when I have errors the error bag is empty and so is the old input one.
I have noticed in the Laravel Debugbar that 2 requests are actually being made : the form's POST (the response of which contains the error bags etc) followed by a GET to the route where the form is.
This SO post (questions ID is 38058223, sorry can't link it on my first day !) showcases the exact same behavior, however I cannot rely on removing the web middleware (which didn't work anyways).
Do you guys have any idea of what the issue might be and how to solve it ?
Thank you in advance for your help ! Yours
@Tray2 Okay so the problem wasn't related to my code at all.
At least not the controller nor the view :)
Someone in my team had added the StartSession middleware in the $middlewares array of the Kernel, removing it solved the error bag issue !
Thanks for you dedicated time and efforts :)
Please or to participate in this conversation.

