populate old html select field in laravel upon failure
Asked Answered
K

5

6

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?

Kistna answered 3/10, 2014 at 5:11 Comment(5)
Do you redirect back with ->withInput()?Josettejosey
yeah i did that, other input field are populated fineKistna
Ah, it's a select. I didn't notice that. Selects don't work that way. See w3schools.com/tags/att_option_selected.asp I use Form::model and Form::select, the Laravel way :)Josettejosey
how about if i don't want to use laravel form class?Kistna
Then you just have to figure out a way to insert selected on the right option. I don't see why you wouldn't use the Laravel form class, but that's me.Josettejosey
B
11

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> 
Bushwhack answered 3/10, 2014 at 5:52 Comment(0)
M
3

I personally think this code is cleaner. There is no right or wrong here, I can understand why some would prefer the Laravel @if @endif statements, I just think they look too visually disturbing.

<option {{ old('gender')=='male' ? 'selected="selected"' : '' }}>Test option</option>
Meggie answered 25/10, 2016 at 17:12 Comment(0)
P
1

This is due to HTML select element.

If you want to assign some default option value to the select element use the attribute selected="selected" for that particular option otherwise by default it will show the first option value.

<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>
Palaeo answered 3/10, 2014 at 5:58 Comment(0)
E
0

This is the way I do, very dynamic.

<select id="gender" name="gender">
    <option>Select</option>
        <option value="M">Male</option>
        <option value="F">Female</option>
</select>

<script>
    var currentGender = null;
    for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
    {
        currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
        if(currentGender.getAttribute("value") == "{{ old("gender") }}")
        {
            currentGender.setAttribute("selected","selected");
        }
    }
</script>
Effluence answered 10/5, 2017 at 20:54 Comment(0)
G
0

I would recommend using Laravel Collective package.

{!! Form::label('gender', 'Gender', ['class' => 'control-label']) !!}
{!! Form::select('gender',
    ['M' => 'Male', 'F' => 'Female'],
    'F', //set default value (or null)
    ['class' => 'form-control', 'required' => true,]
) !!}

It takes care of the old value without using that ugly if-else injection into every select value.

Glutelin answered 10/10, 2018 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.