php - Laravel 在 View 中显示错误

标签 php error-handling laravel laravel-4

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

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/

相关文章:

php - Laravel 4 - 工作台中的一项迁移可以回滚,而其他迁移则不能

php - Laravel 5分组仅显示第一行

PHP网站,我应该开发成Linux发行版而不是Windows吗?

php - 将数据从 Laravel 传递到 Bootstrap 模式

c# - 如何在C#中引发错误消息?

javascript - 无法在 'postMessage' GoogleTagManager 上执行 'Window'

javascript - xmlhttprequest 无法加载文件

php - 警告 : file_put_contents - cannot create file using php

rest - 在将 ember-data 与 RESTAdapters 一起使用时,如何处理 emberJs 中的 HTTP 错误?

javascript - 将 Javascript 变量传递给 Vue