流明 ->withErrors($validator)
Lumen ->withErrors($validator)
我正在尝试显示 div 中的所有错误。
控制器
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors);
}
查看
@if($errors->any())
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
@endif
我得到:
未定义的变量:错误
你需要这个:
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors());
}
注意函数调用 errors()
而不是引用变量。
为了使用它,您必须通过取消注释 bootstrap/app.php
文件中 $app->middleware()
方法调用中的中间件来启用会话。
$errors
在我的视图中也是未定义的,直到我在 bootstrap\app.php
中取消注释以下行:
$app->middleware([
Illuminate\Session\Middleware\StartSession::class,
Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);
目前,关于验证的 Lumen 文档告诉您 $errors 变量在您的视图中总是可用。显然,当你使用它时情况并非如此 'out of the box'.
我正在尝试显示 div 中的所有错误。
控制器
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors);
}
查看
@if($errors->any())
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
@endif
我得到:
未定义的变量:错误
你需要这个:
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors());
}
注意函数调用 errors()
而不是引用变量。
为了使用它,您必须通过取消注释 bootstrap/app.php
文件中 $app->middleware()
方法调用中的中间件来启用会话。
$errors
在我的视图中也是未定义的,直到我在 bootstrap\app.php
中取消注释以下行:
$app->middleware([
Illuminate\Session\Middleware\StartSession::class,
Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);
目前,关于验证的 Lumen 文档告诉您 $errors 变量在您的视图中总是可用。显然,当你使用它时情况并非如此 'out of the box'.