0

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 Answers1

0

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.

  • `$errors` is always available if you use the `ShareErrorsFromSession` middleware. https://laravel.com/docs/5.4/validation#quick-displaying-the-validation-errors – Marwelln Aug 03 '17 at 19:52
  • yes, but your 'errors' item in the error bag will not make it into that if you don't add it by yourself in this case. I may be wrong though, only if that is not the issue you are facing – Oluwatobi Samuel Omisakin Aug 03 '17 at 19:54
  • My bad.. withErrors does not work as well.. I will update my post – Ioannis Karagiannis Aug 03 '17 at 19:57
  • pardon my mistake, yes the `$errors` is always available since the validation is done, but I don't think using `\Session::flash('errors', $e->getMessage());` will pass this error into the `$errors` variable. – Oluwatobi Samuel Omisakin Aug 03 '17 at 19:59
  • Thank you all for answering. Just to be clear : Validation errors, withErrors and session flash messages are all dead.. Which means that they return nothing iterable.. I have updated my post to display code for both "withErrors" and flash messages – Ioannis Karagiannis Aug 03 '17 at 20:05
  • you made a mistake with the withErrors, it needs two arguments not one i.e the field and the value. Okay I cannot tell for now, maybe you app is having some issues. Just try that first and see. Its weird that simply adding a value to the session is not showing. hmm – Oluwatobi Samuel Omisakin Aug 03 '17 at 20:08
  • 1
    I suspect that it has something to do with my middleware "auth_pabl". It used to work though.. I also believe that withErrors('Error message') is ok.. Just a string which will be added in $errors array. I did not have any problems using it this way so far. – Ioannis Karagiannis Aug 03 '17 at 20:17