在Laravel 5.3中针对每个字段显示错误

时间:2016-09-07 08:52:39

标签: php laravel laravel-5.3

我有一份带有一些验证的表格。在验证之后,我需要针对每个输入字段显示错误,并且我尝试使用$errors->first('organizations')来执行此操作,但这似乎不起作用,我在屏幕上打印了$errors->first('organizations')。以下是我的代码片段。有人可以指出我做错了吗?

<div class="form-group has-feedback {{ $errors->has('organization') ? 'has-error' : '' }}">
    <label class="control-label" style="{{ $errors->has('organization') ? '' : 'display:none !important;' }}" for="organization"><i class="fa fa-times-circle-o"></i> {{ $errors->has('organization') ? '$errors->first("organization")' : '' }}</label>
    <select class="form-control" placeholder="Organization" id="organization" name="organization">
        <option value="0" style="display: none !important;" disabled selected>Organization</option>
        @foreach ($organizations as $organizations)
            <option value="{{ $organizations->orgid }}">{{ $organizations->orgname }}</option>
        @endforeach
    </select>
</div>

3 个答案:

答案 0 :(得分:1)

所以我自己找到了答案,这似乎是处理错误消息的更好方法。只是把它留给其他新来的人。

<div class="form-group has-feedback {{ $errors->has('organization') ? 'has-error' : '' }}">
        @if($errors->has('organization'))
            <label class="control-label" for="organization"><i class="fa fa-times-circle-o"></i> {{ $errors->first('organization') }}</label>
        @endif
        <select class="form-control" placeholder="Organization" id="organization" name="organization">
            <option value="0" style="display: none !important;" disabled selected>Organization</option>
            @foreach ($organizations as $organizations)
                <option value="{{ $organizations->orgid }}">{{ $organizations->orgname }}</option>
            @endforeach
        </select>
      </div>

答案 1 :(得分:0)

您可以尝试使用我在视图文件中使用的代码

 <div class="form-group @if ($errors->has('email')) has-error @endif has-feedback">
            <input type="email" class="form-control" name="email" id="email" placeholder="Email" >
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
          @if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> @endif
     </div>

希望这会有所帮助。

答案 2 :(得分:0)

你应该写这个。这将解决您的问题

<div class="form-group has-feedback {{ $errors->has('organization') ? 'has-error' : '' }}">
   @if($errors->has('organization'))
       <label class="control-label" for="organization"><i class="fa fa-times-circle-o"></i> {{ $errors->first('organization') }}</label>
   @endif
   <select class="form-control" placeholder="Organization" id="organization" name="organization">
       <option value="0" style="display: none !important;" disabled selected>Organization</option>
       @foreach ($organizations as $organizations)
          <option value="{{ $organizations->orgid }}">{{ $organizations->orgname }}</option>
       @endforeach
   </select>
</div>