I am using Laravel FormRequest
to validate forms but the ErrorBag
is always empty. I found out this is a common issue since Laravel puts all routes in the web middleware by default (yes, I read this: ErrorBag is always empty in Laravel 5.2).
Thing is I did not specified this middleware group inside my routes/web.php
file, which is supposed to be the problem.
But I am using namespaces, and I think this could maybe be the source of the issue.
Route:
Route::group(['namespace' => 'Pages'], function () {
Route::post('/administration/add-member', 'AdminController@addMember');
});
FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class AddMemberRequest extends FormRequest
{
public function authorize()
{
return (Auth::check() && Auth::user()->hasAllRights());
}
public function rules()
{
return [
'firstname' => 'required|alpha|max:255',
'lastname' => 'required|alpha|max:255',
'email' => 'required|email|unique:users|max:255',
];
}
}
My controller does nothing incredible for the moment:
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
if (Auth::check() && !Auth::user()->hasAllRights())
throw new NotFoundHttpException();
}
public function addMember(AddMemberRequest $request)
{
var_dump($request->all());
}
The data comes from this form (I simplified it):
<form id="add-member" class="mt-3" method="post" action="{{ url('/administration/add-member') }}">
@csrf
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<select name="season">
<option value="" selected>None</option>
ect...
</select>
<button id="btn-validate" type="submit" class="btn btn-success"><i class="fas fa-check mr-1"></i>Add member</button>
Maybe there is something to do in my config files or something?