如果我的验证失败,我会这样做:
return Redirect::back()->with('validation', $validation->errors->all());
我也在用:
$restful = true;
所以当我在 get_edit()
时 - 我收到一个错误,在生成我的 View 时没有 $validation 变量,在 post_edit()
-一切都很好,因为它返回一个有错误的重定向...
这是我的观点:
<? foreach($validation as $e): ?>
<div><?= $e; ?></div>
<? endforeach; ?>
undefined variable $validation,现在我正试图将它放在 Router::before 上
Route::filter('before', function()
{
View::share('validation', array());
});
所以变量存在但为空,但现在出现了一个新问题,每次执行此过滤器后,它都会覆盖生成我的 post_edit()
的那些 $validation
,也我在我的 View 中看到了一个变量 $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);
在您的 View 中,您始终可以使用 redirect('register')->withErrors($validator)$errors
,而无需将它们绑定(bind)到 View 。
关于php - Laravel 在 View 中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851685/