2

我正在尝试在 div 中显示所有错误。

控制器

if ($validator->fails()) {
        return redirect('/')->withErrors($validator->errors);
}

看法

@if($errors->any())
    @foreach($errors->all() as $error)
        {{ $error }}
    @endforeach
@endif

我得到:

未定义的变量:错误

4

2 回答 2

5

你需要这个:

if ($validator->fails()) {
    return redirect('/')->withErrors($validator->errors());
}

注意函数调用errors()而不是引用变量。

为了使用它,您必须通过在文件的$app->middleware()方法调用中取消注释中间件来启用会话bootstrap/app.php

于 2015-05-20T17:32:22.150 回答
3

$errors在我的观点中也没有定义,直到我取消注释以下行bootstrap\app.php

$app->middleware([
     Illuminate\Session\Middleware\StartSession::class,
     Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);

目前有关验证的 Lumen 文档告诉您 $errors 变量始终在您的视图中可用。显然,当您“开箱即用”使用它时,情况并非如此。

于 2015-06-27T08:37:43.997 回答