3

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

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

我也在使用:

$restful = true;

所以当我打开时get_edit()- 我收到一个错误,即生成我的视图时没有 $validation 变量,当我进入时post_edit()- 一切正常,因为它返回一个带有错误的重定向......

这是我的观点:

<? foreach($validation as $e): ?>

<div><?= $e; ?></div>

<? endforeach; ?>

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

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

所以变量存在但为空,但现在出现了一个新问题,每次执行此过滤器后,它都会覆盖那些$validation生成 my的变量,而且我在我的视图中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

}
4

1 回答 1

6

你读过文档吗?http://laravel.com/docs/5.0/validation#error-messages-and-views

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

于 2013-04-06T20:35:24.863 回答