Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 $errors not appearing in Blade

So I'm following along with the Laravel 5 fundamentals tutorial and I am stuck on the form validation. I have followed along exactly with the tutorial but I am getting a Undefined variable: errors in my create articles view.

In the tutorial I am following and what I have found online they say the errors variable is always there in the blade file for you to use so I don't know what i am doing wrong?

Any help would be appreciated! loving Laravel except for this error!

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

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Http\Requests;
use App\Http\Requests\UserRequest as UserRequest;
// use App\Http\Requests\CreateArticleRequest as CreateArticleRequest;
use App\Http\Controllers\Controller;
use Illuminate\View\Middleware\ErrorBinder;

class UserController extends Controller
{
    public function create(){
      return view('pages.signUp');
    }

    public function store(UserRequest $request){
      User::create($request->all());
      return 'the user has been registered!';
      return view('user.profile');
    }

}

Request validation

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'country' => 'required',
            'password' => 'required|min:6',
            'confirm_password' => 'required|same:password',
            'height' => 'required',
            'weight' => 'required',
        ];
    }
}
like image 249
Neal Crowley Avatar asked Dec 23 '15 15:12

Neal Crowley


3 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...
    });
    
like image 69
user1669496 Avatar answered Oct 02 '22 04:10

user1669496


Solved

You may change any one of the following:

1. put your working route (app/http/routes.php) on

Route::group(['middleware' => ['web']], function () { // Here like Route::get('/', 'TodoController@index'); Route::post('/', 'TodoController@store'); });

Screenshot -

Screendshot 1

2. Move your protected $middlewareGroups web (app/Http/Kernel.php) on protected $middleware = []

Screenshot -

screenshot2

like image 37
Md. Khairullah Nayan Avatar answered Oct 02 '22 04:10

Md. Khairullah Nayan


This is solution:

Change the defination of your Route groups with a middleware, from :

Route::group(['middleware' => 'web'], function () {

to

Route::group(['middlewareGroups' => 'web'], function () {

Source: https://github.com/laravel/framework/issues/13000

like image 37
Yuri Avatar answered Oct 02 '22 05:10

Yuri