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?
Also, you can use the
?
operator to avoid having to use@if @else @endif
syntax. Change:Simply to:
After Playing around a bit I came up with this and it seems to work just splendidly
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
inline but man that looks ugly to me so long story short I am using the following instead
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.
The solution is to compare
Input::old()
with the$key
variable.Best way to do is following
My solution here is to loop, just to avoid duplicate option