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>
8
  • have you tried dumping the errors? there is a possibility that there is no errors..
    – flex_
    Aug 3, 2017 at 19:55
  • @loannisKaragiannis. I think this may be your solution: stackoverflow.com/a/36691774/5704410 Aug 3, 2017 at 20:26
  • Unfortunately not.. $errors is still empty. protected 'bags' => array (size=0) empty . Plus, web middleware is passed twice now, in my route:list Aug 3, 2017 at 20:51
  • @flex_ dumping the errors give me an empty bag Aug 3, 2017 at 21:05
  • @OmisakinOluwatobi Can you please check out the Edit 2..? Even this simple test does not return my $errors as expected Aug 3, 2017 at 21:23

1 Answer 1

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.

7
  • $errors is always available if you use the ShareErrorsFromSession middleware. laravel.com/docs/5.4/…
    – Marwelln
    Aug 3, 2017 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 Aug 3, 2017 at 19:54
  • My bad.. withErrors does not work as well.. I will update my post Aug 3, 2017 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. Aug 3, 2017 at 19:59
  • 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. Aug 3, 2017 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.