Laravel 在视图中显示错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15851685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel show errors in view
提问by Hello
if my validation fails I do this:
如果我的验证失败,我会这样做:
return Redirect::back()->with('validation', $validation->errors->all());
also I am using:
我也在使用:
$restful = true;
so when I am on get_edit()
- I'am getting an error that there are no $validation variable when generating my view, when in post_edit()
- its all okay because its returns a redirect with errors...
所以当我打开时get_edit()
-我收到一个错误,即在生成我的视图时没有 $validation 变量,当进入时post_edit()
- 一切都好,因为它返回一个带有错误的重定向......
this is my view:
这是我的观点:
<? foreach($validation as $e): ?>
<div><?= $e; ?></div>
<? endforeach; ?>
undefined variable $validation, right now I'am trying to put it on the Router::before
未定义的变量 $validation,现在我正试图把它放在 Router::before 上
Route::filter('before', function()
{
View::share('validation', array());
});
so the variable exists but is empty, but now arises a new problem, everytime after this filter executes it overrides those $validation
that generates my post_edit()
, also i've seen a variable $errors
in my view but is ever empty, i don't how to use it, can you help me?
所以变量存在但为空,但现在出现了一个新问题,每次执行此过滤器后,它都会覆盖那些$validation
生成 mypost_edit()
的变量$errors
,而且我在我的视图中看到了一个变量但一直是空的,我不知道如何使用它, 你能帮助我吗?
so shortly my problem is:
所以很快我的问题是:
public function get_edit($id)
{
//generate my view with all nessesary data, but i can't generate here an error variable
// or its better to put it in one place to globally share it in the views, otherwise i am //getting an error
}
public function post_edit($id)
{
//validating $_POST data, if there is an error redirect it back to the get_edit() WITH a //variable containing errors
}
回答by Barryvdh
Did you read the docs? http://laravel.com/docs/5.0/validation#error-messages-and-views
你读过文档吗?http://laravel.com/docs/5.0/validation#error-messages-and-views
You can use return Redirect::back()->withErrors($validation);
In your views, you can always use redirect('register')->withErrors($validator)$errors
, without binding them to the view.
您可以return Redirect::back()->withErrors($validation);
在您的视图中使用,您始终可以使用 redirect('register')->withErrors($validator) $errors
,而无需将它们绑定到视图。