2

I've look all around SO for similar questions and tried every answers. The validator checks the rules and redirects fine. But the $errors variable is still empty in my views.

Controller:

public function postSlidesAdd(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'priority' => 'required',
            'text' => 'required',
            'image' => 'required'
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator);
        }
        //insert to db and redirect back
    }

Routes:

Route::group(['middleware' => ['web']], function () {
    Route::post('/admin/slides/add', [
        'uses' => 'SitesController@postSlidesAdd',
        'as' => 'admin.slides.add'
    ]);
});

View:

<div class="">
            @if(count($errors)>0))
                <ul>
                    @foreach($errors->all() as $error)
                        <li>{{$error}}</li>

                    @endforeach
                </ul>

            @endif
        </div>

I think I'm missing something small and important, please help. I'm using Laravel 5.2.29

UPDATE: If this is of any help; In my controller:

if ($validator->fails()) {
        dd($validator->errors());
        return back()->withErrors($validator);
    }

This dumps an array 'messages' and inside is another array containing [input names => error messages]. I think this is the array to be sent to the views, but it doesn't get through. Help please what Am I doin wrong.

4
  • I think it's $validator->message()? Commented Apr 8, 2016 at 2:46
  • Thank you for your quick response, but message() method does not exist in the validator; Commented Apr 8, 2016 at 3:34
  • ow. try to make it messages() Commented Apr 8, 2016 at 3:37
  • messages method still not found in class sir. Commented Apr 8, 2016 at 5:36

2 Answers 2

5

In app\Http\Kernel.php move \Illuminate\Session\Middleware\StartSession::class from the 'web' $middlewareGroups to $middleware

Sign up to request clarification or add additional context in comments.

Comments

0

There is an error with your view file; you have an extra parenthesis. Try changing this:

@if(count($errors)>0))

To this:

@if(count($errors) > 0)

Also, in your controller, try changing:

return redirect()->back()->withErrors($validator);

To this:

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

6 Comments

Thank you for your quick response. I updated it and still no data in $errors.
Hi Sir, i updated it. still empty. If this of any help; in my controller: if ($validator->fails()) { dd($validator->errors()); return back()->withErrors($validator); } It returns an array of 'messages' inside is another array with the [input names => error messages].
Try running {{ dd($errors) }} before the if statement in your view
i put it inside @if(isset($errors)) otherwise the view won't load. It returned ViewErrorBag {#182 ▼ #bags: [] } an empty array
Are you sure valid errors are occurring? What form fields are you submitting with? What does dd($request->all()) print out in your controller?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.