简体   繁体   English

Laravel在视图中显示错误

[英]Laravel show errors in view

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 undefined变量$ validation,现在我试着把它放在Router ::之前

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? 所以变量存在但是是空的,但是现在出现了一个新问题,每次在这个过滤器执行之后它会覆盖那些生成我的post_edit() $validation ,同样我在我的视图中看到变量$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

}

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); 您可以使用return Redirect::back()->withErrors($validation); In your views, you can always use redirect('register')->withErrors($validator) $errors , without binding them to the view. 在您的视图中,您始终可以使用重定向('register') - > withErrors($ validator) $errors ,而不将它们绑定到视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM