0

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?

MuZak
  • 181
  • 5
  • 14
  • You can run `php artisan route:list` and look at the middlewares that affect the route(s) you're having trouble with. It's likely you do not have the session middleware enabled for that route (the session is used to flash your errors back to the next page) – deefour Sep 05 '19 at 17:04
  • What version of Laravel are you using? `routes.php` is a very old thing – Salim Djerbouh Sep 05 '19 at 17:05
  • Caddy DZ I am using the last version. I do not have a `routes.php` file, I just picked up this from a post without thinking... I edited my post to correct this. – MuZak Sep 05 '19 at 17:18
  • deefour the route I am having trouble with has web and auth middlewares. Should it also have a session middleware? – MuZak Sep 05 '19 at 17:25

0 Answers0