dxcoder1 dxcoder1 - 1 month ago 21
PHP Question

populate old html select field in laravel upon failure

i have a form with a select input, validation is fine, but upon failure the select field doesn't populate the old value
here is my select field

<div class="control-group">
<label class="control-label">Gender :</label>
<div class="controls">
<select name="gender" value= "{{ Input::old('gender') }}">
<option>Select</option>

<option value="M">Male</option>
<option value="F">Female</option>
</select>
</div>
</div>


how can i solve this?

Answer

If you don't want to use Laravel Form build, you need to do it this way:

 <div class="control-group">
   <label class="control-label">Gender :</label>
    <div class="controls">
        <select  name="gender">
            <option>Select</option>

                <option value="M" @if (Input::old('gender') == 'M') selected="selected" @endif>Male</option>
                <option value="F" @if (Input::old('gender') == 'F') selected="selected" @endif>Female</option>
        </select>
    </div>
</div>