Laravel 5.1:无法在模板中首先显示$ errors->

时间:2015-11-03 03:28:56

标签: php laravel laravel-5 laravel-5.1

我有以下html:

    <div class="form-group
      @if( $errors->has('question') )
        has-error has-feedback
      @endif
    ">
    <label for="question" class="col-md-4 control-label">Question</label>
    <div class="col-md-6">
     <input type="text" class="form-control c-square c-theme"
       id="Idquestion" name="question" value="{{Request::old('question')}}" placeholder="question text" >
   @if ($errors->has('question'))
      <span class="glyphicon glyphicon-remove form-control-feedback">
   {!! $errors->first('question') !!}
     </span>
  @endif

  <?php echo 'PHP ERRORS: '. print_r ($errors->first('question')) ; ?>
   @if( $errors->has('question'))
     <span class="glyphicon glyphicon-remove form-control-feedback">
     {{ $errors->first('question') }}
     </span>
  @endif
    </div>
  </div>

 <div class="form-group">
  <label for="answer" class="col-md-4 control-label">Answer</label>
  <div class="col-md-6">
    <input type="text" class="form-control c-square c-theme" id="Idanswer" name="answer" placeholder="input">
    </div>
 </div>

我的问题是,只有$ errors-&gt; all()才有效。但$ errors-&gt;有('question'),$ errors-&gt; first('question')无效。

以下是显示html代码的错误:

    @if (count($errors) > 0)
    <div class="c-center alert alert-danger" role="alert">
        <ul>
            <li>Errors: {{  print_r ($errors)  }}</li>
            <li>Total Errors: {{  count($errors)  }}</li>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
            <li>Question Error: {{ $errors->first('question') }} </li>
        </ul>
    </div>
@endif

错误的Html代码,显示如下:

  Errors: Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( [default] => Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [0] => Array ( [0] => The question field is required. ) [1] => Array ( [0] => The answer field is required. ) ) [format:protected] => :message ) ) ) 1
Total Errors: 2
The question field is required.
The answer field is required.
Question Error:

有人可以指导我做错了什么以及如何纠正。

2 个答案:

答案 0 :(得分:2)

试试这个,

刀片上的

链接如下。

if ($validation->fails()) {
            return redirect()->route('your route path')->withErrors($validation)->withInput();
        }

在你的控制器上就像。

bu1<- function(j){
sum(linkinc_lev1$gdp*(1/(1+ (linkinc_lev1$use_gro*(1+j/100))))
}

bu1<- function(j){
sum(linkinc_lev2$gdp*(1/(1+ (linkinc_lev2$use_gro*(1+i/100))))
}

它适用于所有单个输入字段,也可填充该字段中的旧数据。

希望它有所帮助。

答案 1 :(得分:0)

Laravel错误包

表单名称可以传递给MessageBag错误,允许您检索特定表单的错误消息。

将名称作为第二个参数传递给withErrors

return redirect('viewName')->withErrors($validator, 'formName');

视图 (刀片)中,您可以使用以下名称访问MessageBag

{{ $errors->formName->first('formFieldName') }}

请记住,您在 first() 中传递了该字段的名称。 检查您是否有名称 &#34;问题&#34;

的表单元素

参考文档Laravel Error Bag

希望这有用。