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



我有以下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->all()在工作。但是$errors->有('question'),$errors->first('questions')不起作用。

以下是我显示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: IlluminateSupportViewErrorBag Object ( [bags:protected] => Array ( [default] => IlluminateSupportMessageBag 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:

有人能告诉我我做错了什么以及如何纠正吗。

试试这个,

在你的刀刃上有下面的链接。

 <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
            <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }} control-required">
            {!! Form::label('title', 'Title') !!}<span class="mand_star"> *</span>
                {!! Form::text('title', isset($news->title) ? $news->title : Input::old('title'), [
                    'class'       => 'form-control',
                    'placeholder' => 'News Title',
                    'required'    => 'required'
                ]) !!}
               <span class="error_span"> {{ $errors->first('title') }}</span>
            </div>
        </div>

在你的控制器上。

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

它适用于所有单独的输入字段,并在该字段中填充旧数据。

希望能有所帮助。

Laravel错误包

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

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

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

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

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

请记住,您在first()中传递字段的名称。检查是否有名称为"问题"的表单元素

参考文件Laravel错误包

希望这会有所帮助。

最新更新