validationlaravel-8laravel-validationerror-messaging

Select field validation


<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>

The category_id is defined as the required field in my Request php file ('category_id' => 'required')

But when I submit the form without any input to any field, all fields generate error messages by highlighting the field, except this SELECT field.


Solution

  • It passes the validation because your are sending a value in that field.

    Change this

    <option value="177">Select a category</option>
    

    to

    <option selected disabled>Select a category</option>
    

    then it should not pass the validation.