8

This is weird. I've been googling all day trying to find a solution for my problem and most of solutions don't work for me due to different versions or different request - controller handling.

What's happening is this.

I have a form:

<div class="form-group">
    Name *
    {!! Form::text('name', '', ['class'=>'form-control', 'placeholder'=>'Required field']) !!}
</div>

And a Request:

class ContactFormRequest extends Request
{

    public function authorize()
    {
        return true;
    }


    public function rules()
    {
        return [
            'name' => 'required|max:64',
            'email' => 'required|email|max:128',
            'message' => 'required|max:1024',
        ];
    }
}

I'm leaving the name field blank so it fails validation, and it should return to the contact form page and show the errors:

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

It shows nothing! If I vardump the $errors variable, I get this:

object(Illuminate\Support\ViewErrorBag)[161]
  protected 'bags' => 
    array (size=0)
      empty

If I fill the form field properly it successfully sends me to the success page and everything works perfect. All I need now is to make this error thing work properly :S

Thank you in advance!

Juan Bonnett
  • 1,823
  • 1
  • 15
  • 33
  • Are your controllers bounded to a route part of the __web middleware group__? If not, try to bound them, but I can't see nothing wrong in your code... – Hammerbot Feb 14 '16 at 02:16
  • I assume your controller's method includes ContactFormRequest in the parameter list? Do you redirect back to the form yourself or do you rely on validator's default action? – Denis Mysenko Feb 14 '16 at 02:17
  • Yes. All my routes are grouped with the Middleware 'web'. And my controller method in its parameter has ContactFormRequest as the type! – Juan Bonnett Feb 14 '16 at 02:31
  • what does `var_dump($errors->all());` say? – Axalix Feb 14 '16 at 02:38
  • array (size=0) empty - So weird, and I'm leaving the required fields blank, it redirects back, but shows no errors! :S – Juan Bonnett Feb 14 '16 at 02:43
  • It could be, because you are missing a call for validation. There should be something like this in your controller: `if ($validator->fails()) {` – Axalix Feb 14 '16 at 02:45
  • If I give you an URL with my controller snippet, could you help me place it please? https://codeshare.io/m1CJb – Juan Bonnett Feb 14 '16 at 02:49

1 Answers1

4

This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.

There are two ways to fix this:

1-In your kernel.php file, you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

2-You can wrap all your web routes with a route group and apply the web middleware to them.

Route::group(['middleware' => 'web'], function() {
    // Place all your web routes here...
});

See this
laravel-5-2-errors-not-appearing-in-blade

Community
  • 1
  • 1
paranoid
  • 6,799
  • 19
  • 49
  • 86