Define the selected option with the old input in L

2019-01-17 20:56发布

I have this code:

<select required="required" class="form-control" name="title">
    <option></option>
    @foreach ($titles as $key => $val)
        @if (stristr($key, 'isGroup'))
            <optgroup label="{{ $val }}">
        @else
        <option value="{{ $key }}">{{ $val }}</option>
        @endif
    @endforeach
    </select>

So when the form have errors i use the line Redirect::route('xpto')->withInput()->withErrors($v). But i can't re-populate the select fields. Any way to do this without using JavaScript for example?

11条回答
ゆ 、 Hurt°
2楼-- · 2019-01-17 21:24

Also, you can use the ? operator to avoid having to use @if @else @endif syntax. Change:

@if (Input::old('title') == $key)
      <option value="{{ $key }}" selected>{{ $val }}</option>
@else
      <option value="{{ $key }}">{{ $val }}</option>
@endif

Simply to:

<option value="{{ $key }}" {{ (Input::old("title") == $key ? "selected":"") }}>{{ $val }}</option>
查看更多
姐就是有狂的资本
3楼-- · 2019-01-17 21:24

After Playing around a bit I came up with this and it seems to work just splendidly

<select name="options[]" id="options" class="form-control" multiple>
    @foreach($settings->includes->get('optionList') as $option)
        <option value="{{ $option->id }}" {{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}>{{ $option->name }}</option>
    @endforeach
</select>

I may be 100% wrong in leveraging the collect function but it works fine on many of my tests. After seeing a few other posts on the site I saw someone recommend leveraging the in_array($needle, $array) function but after noticing that if my old('options') was null it would error out because it requires in_array requires, bet you guessed an array. So after finding the solution to that albeit ugly solution I played with the collect method because after all we are using larval right! well anyway the ugly solution is as follows

@if (old("options")){{ (in_array($option->id, old("options")) ? "selected":"") }}@endif

inline but man that looks ugly to me so long story short I am using the following instead

{{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}

Hope this helps others!!

This does not seem to work for a non multiple select field ill get back with one that does work for that though.

查看更多
男人必须洒脱
4楼-- · 2019-01-17 21:29

The solution is to compare Input::old() with the $keyvariable.

@if (Input::old('title') == $key)
      <option value="{{ $key }}" selected>{{ $val }}</option>
@else
      <option value="{{ $key }}">{{ $val }}</option>
@endif
查看更多
一纸荒年 Trace。
5楼-- · 2019-01-17 21:29
<select>
    @if(old('value') =={{$key}})
     <option value="value" selected>{{$value}}</option>
    @else
     <option value="value">{{$value}}</option>
    @endif
</select>
查看更多
Lonely孤独者°
6楼-- · 2019-01-17 21:30

Best way to do is following

<select class="form-control" name="team" id="team">
 <option value="">---------Choose Team---------</option>
       @foreach($teams as $team)
<option value="{{$team->id}}" {{(old('team')==$team->id)? 'selected':''}}>{{$team->name}}</option>

@endforeach
</select>

eg.<select name="title">
<option value="1"  {{ old('title') == 1 ? 'selected' : '' }}>
    Item 1
</option>
<option value="2" {{ old('title') == 2 ? 'selected' : '' }}>
    Item 2
</option>

</select>
查看更多
虎瘦雄心在
7楼-- · 2019-01-17 21:31

My solution here is to loop, just to avoid duplicate option

                            <select class="form-control" name="status" >
                              <?php $lists = ['Current', 'Win', 'Lose']; ?>

                              @foreach($lists as $list)
                              <option value={{$list}} {{(old('status') == $list?'selected':'')}} >{{$list}}</option>
                              @endforeach

                            </select>
查看更多
登录 后发表回答