正如您所料,当验证失败时,我创建了一个重定向。
return Redirect::to('search')->withErrors($v->messages())
我可以在视图中访问它而没有问题,但我想做一些不同的事情。我有一个ErrorPartial.blade.php,我希望将其传递给我的搜索视图。
return View::make('search.searchForm')
->with('title', 'Search Page')
->with('components', Subject::select('Component')->distinct()->get())
->with('measurementRow',$measurementRow)
->with('races', Race::parseRaceTable())
->with('errorPartial', View::make('errorPartial')
->with('errors',$v->messages())
->render())
;
问题是我在此控制器功能中无法访问$ v。我可以访问将要传递给视图的错误吗?我试过这个:
return Redirect::to('search')->withErrors($v->messages())
->with('v', $v);
但是我收到了这个错误。
Serialization of 'Closure' is not allowed
我可以在搜索视图中创建部分视图,但我想知道他们是否有办法这样做。如果有人知道哪个更有效率或GPP那么我也不介意知道这一点。
由于
答案 0 :(得分:8)
回答这个问题,将其作为一个悬而未决的问题关闭。
Laravel将错误存储在Session中,可以这样访问:
$errors = Session::get('errors');
答案 1 :(得分:3)
Laravel将错误存储在会话中。
刀片中可用的所有功能都来自ViewErrorBag类。
use Illuminate\Support\ViewErrorBag;
...
$errors = session()->get('errors', app(ViewErrorBag::class));
首选此方法,因为如果会话中没有错误,它将返回空错误包。这意味着您可以在此对象上调用$errors->any()
,而不会期望出现错误提示
在null上调用成员函数any()
您始终可以按照以下建议在源代码中找到实现
$errors
变量由Illuminate\View\Middleware\ShareErrorsFromSession
中间件组提供的web
中间件绑定到视图。应用此中间件时,$errors
变量将始终在您的视图中可用,从而使您可以方便地假设$errors
变量始终被定义并且可以安全地使用。
答案 2 :(得分:0)
这是另一种选择。从默认的消息包中获取错误键。
use Illuminate\Support\Facades\Session;
public function tranfers(Request $request) {
...
// test the error exists
$has_tranfer_error = (Session::get('errors') && Session::get('errors')->getBag('default')->has('insufficient_funds'));
...
}
public function make_transfer(Request $request) {
...
// Add the error
return back()->withErrors(['insufficient_funds' => 'The balance is too low to make a transfer'])->withInput();
}