32

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',
        ];
    }
}
4

11 回答 11

41

这是 5.2 升级的一个重大问题。正在发生的事情是负责使该errors变量可用于所有视图的中间件没有被使用,因为它已从全局中间件移动到web中间件组。

有两种方法可以解决此问题:

  1. 在您的kernel.php文件中,您可以将中间件\Illuminate\View\Middleware\ShareErrorsFromSession::class移回protected $middleware属性。

  2. 您可以使用路由组包装所有 Web 路由并将web中间件应用于它们。

    Route::group(['middleware' => 'web'], function() {
        // Place all your web routes here...
    });
    
于 2015-12-23T16:17:50.740 回答
24

解决了

您可以更改以下任何一项:

1.把你的工作路线(app/http/routes.php)放在

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

截屏 -

截图 1

2. 继续protected $middlewareGroups web (app/Http/Kernel.php)前进protected $middleware = []

截屏 -

截图2

于 2016-01-20T12:55:06.570 回答
19

这是解决方案:

使用中间件更改路由组的定义,

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

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

来源:https ://github.com/laravel/framework/issues/13000

于 2016-07-12T18:44:43.973 回答
18

简单地说,你必须移动:

\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,

protected $middlewareGroupsprotected $middleware

于 2016-03-30T14:30:47.393 回答
7
  1. 只需, 'middleware' => 'web'routes.php页面Route::group(array('prefix' => 'user', 'middleware' => 'web'), function()中删除或

  2. 移动

    \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,

内核.php页面中的protected $middlewareGroupsprotected $middleware

正如 Laravel 文档所说:

注意:如果您的 Laravel 副本中的 RouteServiceProvider 已经包含 web 中间件组中的默认路由文件,则无需手动将该组添加到您的 routes.php 文件中。

所以从routes.php文件中删除将是正确的方法。

于 2016-05-05T12:20:58.220 回答
5

发布此内容可能对其他人有用,

正如 Smartrahat 在第一个解决方案中提到的那样,在您的Kernel.phpfile( app/Http/Kernel.php)中\Illuminate\View\Middleware\ShareErrorsFromSession::class 从属性移动$middlewareGroupsprotected $middleware但同样会开始抛出错误“会话存储未按请求设置”,

也解决这个 \Illuminate\Session\Middleware\StartSession::class,问题$middleware property

于 2016-02-09T08:07:31.863 回答
4

从 5.2 开始,routes.php默认情况下已经在 a ['middleware'=>'web']by的上下文中调用RouteServiceProvider。但是在routes.php默认生成的身份验证路由中,默认情况下Route::group调用仍在发生 - 因此,如果您Route::group从应用程序中删除该声明,routes.php则正确显示错误。

于 2016-03-27T22:26:57.370 回答
4

关于这个问题的几点意见。首先,github 中有一个关于此问题 PFA 的相关错误 https://github.com/laravel/framework/issues/12022

如果你看看格雷厄姆写的最后一条评论,我想这就是我所面临的。对我来说,即使表单发布数据有错误,我也得到了以下等式

boolval(count($errors) === 0) === true

就我而言,我在

\Illuminate\Session\Middleware\StartSession::class

上面的中间件类为给定的请求运行了两次,我不确定为什么它运行了两次,但我认为因此$errors变量正在重置。我正在使用这个配置(我认为这是 Laravel@5.2.43 的默认配置)

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

protected $middlewareGroups = [
    'web' => [
        \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,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

我更改了对我有用的配置,并且$errors变量的计数不为零(上述中间件也每个请求只运行一次)

protected $middleware = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

注意:我的所有路由在webKernel.php 配置更改前后都在中间件组中,我根本没有从web中间件组中移动我的路由。

于 2016-09-12T17:17:05.027 回答
2

更改@foreach($errors->any() as $error)@foreach($errors->all() as $error)

于 2015-12-23T16:15:23.340 回答
0

在这种情况下,您可以参考laravel 5.2kernal.php示例代码并编辑您的文件。移动 this \Illuminate\View\Middleware\ShareErrorsFromSession::class, formmiddlewareGroupsmiddlewareadd \Illuminate\Session\Middleware\StartSession::class, middleware 然后它的工作正常。

于 2016-02-17T10:25:30.657 回答
0

在我们的应用程序中同时存在 Web 和 API 需求,我们不想移动中间件;然而,也许这会奏效:

我们遇到了非常特殊的情况,即flash[]会话$errors数据在标准 laravel 资源方法store()和之间正确传输edit(),但在某些情况下,数据并没有通过几乎相同的方法返回,storeSale()并且editSale()

我们发现在我们的开发和部署环境中,“文件”和“数据库”会话驱动程序在所有情况下都有效,但“cookie”驱动程序没有。

在所有情况下切换到数据库驱动程序解决了这个问题。

于 2016-07-06T18:40:43.947 回答