0
votes

The last couple of days, I face a problem. Validation errors variable comes empty inside blade files.. I am developing a multilingual application in Laravel 5.2.45. So there are the validation error messages in each of them (resources/lang/{locale}/validation.php).

Validation rules live inside a Request file (e.g. ValidateUserRequest) containing the validation rules and declaring authorize as true. I then pass it to the controller. Bellow is my middlewares and part of my routes file.

Any help will be much appreciated

Kernel.php

 protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
 ];

 protected $routeMiddleware = [
    'auth_pabl' => \App\Http\Middleware\AuthenticateBackEnd::class,
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
 ];

routes.php (all controllers inside Admin directory)

Route::auth();

Route::group ( [ 
    'namespace' => 'Admin'
], 
function () {

Route::get ( 'admin/dashboard', [ 

        'uses' => 'PanelController@dashboard',
        'as' => 'admin.dashboard'

] );

/*
 * Authors
 */

Route::get ( 'admin/authors', [ 

        'uses' => 'PanelController@authors',
        'as' => 'admin.authors'

] );

Route::post ( 'admin/authors', [ 

        'uses' => 'AuthorController@store',
        'as' => 'admin.authors.store' 
] );

Route::get ( 'admin/authors/{slug}/edit', [ 

        'uses' => 'AuthorController@edit',
        'as' => 'admin.authors.edit' 
] );

Route::post ( 'admin/authors/{username}/edit', [ 

        'uses' => 'AuthorController@update',
        'as' => 'admin.authors.update' 
] );

Route::get ( 'admin/authors/{username}/delete', [ 

        'uses' => 'AuthorController@delete',
        'as' => 'admin.authors.delete' 
] );

A session message would normally come from something like

\Session::flash('flash_contact', 'Success. Data stored');

return redirect()->route('admin.events.create');

And it is (not) displayed with

@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
    <p>{!! trans('admin/form/placeholders.errors') !!}</p>
    <ul class="errors">
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    </ul>
</div>
@endif
@if(Session::has('flash_contact'))

<div id="success" class="alert alert-success text-center">
    {{Session::get('flash_contact')}}
</div>
@endif

Finally php artisan route:list states that the "web" middleware is present (I have not declared it twice manually)

//Edit 1 (withErrors from controller)

Note here that neither function withErrors give me the expected result. Below is a part of my controller.

public function store(Requests\ValidateEventsRequest $request)
{

    try {
        return $this->dbEvent->add($request);
    } catch (\Exception $e) {

        return redirect()->to('/admin/events/add')->withErrors('Error 
            detected');
    }

}

//Edit 2

Shouldn't this work???

Route::get('flash', function () {

    return redirect()->to('flash2')->withErrors('Where are my errors?');

});

Route::get('flash2', function () {

    return view('flash2');

});

And my flash2.blade.php

<html>
<body>
@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
    <p>{!! trans('admin/form/placeholders.errors') !!}</p>
    <ul class="errors">
    @foreach($errors->all() as $error)
        <li>{{$error}}</li>
    @endforeach
    </ul>
</div>
@endif
this is my flash page
</body>
</html>
1
have you tried dumping the errors? there is a possibility that there is no errors..flex_
@loannisKaragiannis. I think this may be your solution: stackoverflow.com/a/36691774/5704410Oluwatobi Samuel Omisakin
Unfortunately not.. $errors is still empty. protected 'bags' => array (size=0) empty . Plus, web middleware is passed twice now, in my route:listIoannis Karagiannis
@flex_ dumping the errors give me an empty bagIoannis Karagiannis
@OmisakinOluwatobi Can you please check out the Edit 2..? Even this simple test does not return my $errors as expectedIoannis Karagiannis

1 Answers

0
votes

In order to access the $errors to my understanding you have to add it to the ErrorBag so instead you do this:

return redirect()->route('admin.events.create')->withErrors('not_found', $e->getMessage);

Its then you can access $errors variable and find it there in the view.

Update: For those who might find this answer amongst the result of google search:

Based on these two answers: here and here and also the confirmation of OP:

moving the Illuminate\Session\Middleware\StartSession::class to $routeMiddleware was the solution according to the links and it worked.

I hope this is already fixed in Laravel I think so, since the web routes and others are now separated in L5.4.