0
votes

I'm using chatter package for Laravel. but when there is an error while validating. there is no error bag send to the blade. this is the code:

  $validator = Validator::make($request->all(), [
        'title'               => 'required|min:5|max:255',
        'body_content'        => 'required|min:10',
        'chatter_category_id' => 'required',
    ]);

    if ($validator->fails()) {

        return back()->withErrors($validator)->withInput();
    }

blade:

@if (count($errors) > 0)
    <div class="chatter-alert alert alert-danger">
        <div class="container">
            <p><strong><i class="chatter-alert-danger"></i> {{ Config::get('chatter.alert_messages.danger') }}</strong> Please fix the following errors:</p>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    </div>
@endif

kernel:

  protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \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,

];

every time I'm trying to dd the $errors in the blade, there is always empty bag.
Note: every validation working fine, but not this one

2
Do you have input back? - Basheer Kharoti
no, the input also doesnt show - Aldi Jakaria
Try dd(Session::all()); on vendor/laravel at this line github.com/laravel/framework/blob/5.5/src/Illuminate/Http/… - Basheer Kharoti
Try with this: return redirect()->back()->withErrors($validator)->withInput(); instead of return back()->withErrors($validator)->withInput(); - Hiren Gohel
Where's your form code? - Option

2 Answers

1
votes

Comment this out

 // \Illuminate\Session\Middleware\StartSession::class,

And make sure you have a web middleware in your routes.

Route::group(['middleware' => 'web'], function () {
      // Your routes
});
0
votes

try this one dude.

controller

$this->validate(
            $request,
            [
                'title'               => 'required|min:5|max:255',
                'body_content'        => 'required|min:10',
                'chatter_category_id' => 'required',
            ]
        );

if validate is fail it automatically return back to your previous view.

View

@if (count($errors) > 0)
    <div class="chatter-alert alert alert-danger">
        <div class="container">
            <p><strong><i class="chatter-alert-danger"></i> {{ Config::get('chatter.alert_messages.danger') }}</strong> Please fix the following errors:</p>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    </div>
@endif