Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

validation - Laravel MessageBag errors array is empty in view but with content if I kill script

I am trying to return errors back to my view, this is part of my controller TestcategoryController

    $rules =array(
        'name' => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);
    //process
    if($validator->fails()){
        return Redirect::to('testcategory/create')->withErrors($validator);
    }

In the view testcategory/create if I try and output the errors like

        @if($errors->any())         
            {{ $errors->first('name') }}
        @endif

I get nothing. But if I {{dd($errors)}} I get

      object(IlluminateSupportViewErrorBag)#91 (1) { ["bags":protected]=> array(1) { 
      ["default"]=> object(IlluminateSupportMessageBag)#92 (2) 
      { ["messages":protected]=>   array(1) 
      { ["name"]=> array(1) { [0]=> string(27) "The name field is required." } }  
      ["format":protected]=> string(8) ":message" } } }

The only way I am getting the errors is if I kill the script. What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably another issue would be with $errors not being saved to Session variable $errors and nothing being shown in the view.

Here is an example of the same issue: http://laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation

For me the solution defined in the above link worked. Solution: Is in appHttpKernel.php Move IlluminateSessionMiddlewareStartSession::class, from $middlewareGroups to $middleware

Before

enter image description here

After

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...