问题描述
<div class="form-group">
<label for="category-id">Category</label>
<select id="category-id" class="form-control {{$errors->get('category_id') ? 'is-invalid' : ''}}" name="category_id">
<option value="177">Select a category</option>
@foreach ($categories as $category)
<option value="{{$category['id']}}">{{$category['title']}}</option>
@endforeach
</select>
@if ($errors->has('category_id'))
<div class="invalid-Feedback"><strong>{{$errors->first('category_id')}}</strong></div>
@endif
</div>
category_id 定义为我的请求 PHP 文件 ('category_id' => 'required'
) 中的必填字段
但是当我在没有任何字段输入的情况下提交表单时,所有字段都会通过突出显示该字段来生成错误消息,除了这个 SELECT 字段。
解决方法
它通过了验证,因为您在该字段中发送了一个值。
改变这个
<option value="177">Select a category</option>
到
<option selected disabled>Select a category</option>
那么它应该不会通过验证。