Yosef Yosef - 3 years ago 258
PHP Question

Select element not displaying Laravel vaildation errors

i never had this problem before i have a select element with option and in my controller i set it to required and for some reason the validation error does not display the has error class doesn't even get added to the form-group

html and blade

<div class="form-group {{ $errors->has('Test') ? 'has-error' : ''}}">
<label for="Return_Type">Return Type <span class="request-alert"></span></label>
<select name="Test" id="Return_Type" class="form-control">
<option>Choose One...</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
@if($errors->has('Test'))
<span class="help-block">
{{ $errors->first('Test') }}
</span>
@endif
</div>


controller

$this->validate($request, [
'First_Name' => 'required',
'Last_Name' => 'required',
'Email' => 'required',
'Phone' => 'required',
'Fax' ,
'Company' ,
'Marketplace' => 'required',
'Order_Number' => 'required',
'Address_1' => 'required',
'Address_2' ,
'City' => 'required',
'State' => 'required',
'Zip_Code' => 'required',
'Test' => 'required',
'Product_Name' => 'required',
'Quantity' => 'required',
'SKU' => 'required',
]);

Answer Source

You need to add ==> value="" in your first select option, i.e

<option value="">Choose One...</option>

so change your select form part to

<div class="form-group {{ $errors->has('Test') ? 'has-error' : ''}}">
   <label for="Return_Type">Return Type <span class="request-alert"></span></label>
     <select name="Test" id="Return_Type" class="form-control">
          <option value="">Choose One...</option>
              @foreach($categories as $category)
                 <option value="{{ $category->id }}">{{ $category->name }}</option>
              @endforeach
      </select>
      @if($errors->has('Test'))
         <span class="help-block">
              {{ $errors->first('Test') }}
         </span>
       @endif

I hope it helps :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download