Laravel显示视图中的错误


Laravel show errors in view

如果我的验证失败,我这样做:

return Redirect::back()->with('validation', $validation->errors->all());

我也在使用:

$restful = true;

所以当我在get_edit() -我得到一个错误,没有$验证变量时,生成我的视图,当在post_edit() -一切都好,因为它返回一个重定向与错误…

这是我的观点:

<? foreach($validation as $e): ?>
<div><?= $e; ?></div>
<? endforeach; ?>

未定义变量$validation,现在我正试图把它放在Router::

之前
Route::filter('before', function()
{
    View::share('validation', array());
});

所以变量存在,但是空的,但现在出现了一个新的问题,每次这个过滤器执行后,它覆盖了那些$validation,生成我的post_edit(),我也看到了一个变量$errors在我的视图,但一直是空的,我不知道如何使用它,你能帮助我吗?

我的问题是:

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
}

你看文档了吗?http://laravel.com/docs/5.0/validation error-messages-and-views

您可以使用return Redirect::back()->withErrors($validation);在你的视图中,你总是可以使用redirect('register')->withErrors($validator) $errors,而不需要将它们绑定到视图。