lundi 2 novembre 2015

Laravel 5.1: Unable to display $errors->first in template

I have following 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>

My problem is that, only $errors->all() is working. But $errors->has('question'), $errors->first('question') not working.

Following is my error showing html code:

    @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 code for errors, shows me following:

  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:

Can some one guide me what I am doing wrong and how it can be rectified.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire