使用 Laravel / Blade 中的旧输入定义所选选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29148274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define the selected option with the old input in Laravel / Blade
提问by Christopher
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?
因此,当表单有错误时,我使用该行Redirect::route('xpto')->withInput()->withErrors($v)
。但我无法重新填充选择字段。例如,有什么方法可以在不使用 JavaScript 的情况下做到这一点?
采纳答案by Christopher
The solution is to compare Input::old()
with the $key
variable.
解决方案是Input::old()
与$key
变量进行比较。
@if (Input::old('title') == $key)
<option value="{{ $key }}" selected>{{ $val }}</option>
@else
<option value="{{ $key }}">{{ $val }}</option>
@endif
回答by Tim Lewis
Also, you can use the ?
operator to avoid having to use @if @else @endif
syntax. Change:
此外,您可以使用?
运算符来避免使用@if @else @endif
语法。改变:
@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>
回答by G-Rajendra
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" @if (old('gender') == "M") {{ 'selected' }} @endif>Male</option>
<option value="F" @if (old('gender') == "F") {{ 'selected' }} @endif>Female</option>
</select>
回答by Carlos Quinones
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
我在利用 collect 函数时可能 100% 是错误的,但它在我的许多测试中都运行良好。在网站上看到其他一些帖子后,我看到有人建议利用 in_array($needle, $array) 函数,但在注意到如果我的 old('options') 为 null 之后,它会出错,因为它需要 in_array 需要,打赌你猜到了一个数组。因此,在找到了那个虽然丑陋的解决方案的解决方案后,我使用了 collect 方法,因为毕竟我们正在使用幼虫!无论如何,丑陋的解决方案如下
@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.
这似乎不适用于非多选字段,但我会返回一个确实适用于该字段的字段。
回答by 5less
Instead of using Input class you can also use old() helper to make this even shorter.
除了使用 Input 类之外,您还可以使用 old() 助手来缩短它的时间。
<option {{ old('name') == $key ? "selected" : "" }} value="{{ $value }}">
回答by bikash bhandari
I have changed the code to include ''
on the title value since without the quotes it fails to work
我已将代码更改为包含''
在标题值中,因为没有引号它无法工作
<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>
回答by Aleh
<option value="{{ $key }}" {{ Input::old('title') == $key ? 'selected="selected"' : '' }}>{{ $val }}</option>
回答by Riki krismawan
<select class="form-control" name="kategori_id">
<option value="">-- PILIH --</option>
@foreach($kategori as $id => $nama)
@if(old('kategori_id', $produk->kategori_id) == $id )
<option value="{{ $id }}" selected>{{ $nama }}</option>
@else
<option value="{{ $id }}">{{ $nama }}</option>
@endif
@endforeach
</select>
回答by Lord
Laravel 6 or above:just use the old() function for instance @if (old('cat')==$cat->id), it will do the rest of the work for you.
Laravel 6 或更高版本:只需使用 old() 函数,例如@if (old('cat')==$cat->id),它将为您完成其余的工作。
How its works:select tag store the selected option value into its name attribute in bellow case if you select any option it will store into cat. At the first time when page loaded there is nothing inside cat, when user chose a option the value of that selected option is stored into cat so when user were redirected old()function pull the previous value from cat.
它是如何工作的:select 标签将选定的选项值存储到它的 name 属性中,如果您选择任何选项,它将存储到cat 中。第一次加载页面时,cat 中没有任何内容,当用户选择一个选项时,该选项的值存储到 cat 中,因此当用户被重定向时,old()函数从 cat 中提取先前的值。
{!!Form::open(['action'=>'CategoryController@storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
@foreach ($cats as $cat)
@if (old('cat')==$cat->id)
<option value={{$cat->id}} selected>{{ $cat->title }}</option>
@else
<option value={{$cat->id}} >{{ $cat->title }}</option>
@endif
@endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
回答by Milkmannetje
Okay, my 2 cents, using the default value of Laravel's old() function.
好的,我的 2 美分,使用 Laravel 的 old() 函数的默认值。
<select name="type">
@foreach($options as $key => $text)
<option @if((int) old('type', $selectedOption) === $key) selected @endif value="{{ $key }}">{{ $text }}</option>
@endforeach
</select>