Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 not showing form validation errors [duplicate]

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!

like image 416
Juan Bonnett Avatar asked Feb 14 '16 01:02

Juan Bonnett


1 Answers

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

like image 118
paranoid Avatar answered Nov 19 '22 08:11

paranoid