Checkbox array - old('value') Head is not working... how to return old value after validation to this? I already tried so many different ways...
<ul class="optional-features-list">
@foreach ($features as $feature)
@if ($feature->category_name == 'Interior Features')
<li class="checkbox"><label><input id="features" name="feature[{{$feature->id}}]" type="checkbox">{{$feature->feature_name}}</label></li>
@endif
@endforeach
</ul>
here is how it looks data:
array:11 [▼
"_token" => "HzN2Niy33fjSWtPF4kUY003kAGScvvzCUCABV5Ia"
"plate_number" => "any"
"feature" => array:3 [▼
1 => "1"
4 => "4"
7 => "7"
]
@freel - try:
<input id="features" name="feature[{{$feature->id}}]" type="checkbox" value="1" @if (old('feature[$feature->id]') == "1") checked @endif >
EDIT:
just realized, that you are looking for the validation of an array. You need to write custom rule(s) Too lazy to write all down, since it's summarized pretty nicely here:
http://ericlbarnes.com/2015/04/04/laravel-array-validation/
You can't use if/endif inline like that..
But I think this ought to work:
<input id="features"
name="feature[{{$feature->id}}]"
type="checkbox"
value="1"
{!! old("feature[$feature->id]") ? 'checked="checked"' : '' !!}>
@willvincent
You can't use if/endif inline like that..
but it's working, so obviously i can ;)
Hmm.. I've tried and it didn't work, maybe blade supports it now. Or do you have it on separate lines?
Hello guys. Non of your suggestions worked... any other ideas...
maybe by form not returning old('key') value? how to debug? with dd(old('key')) geting null ....
Thx first example working perfect! thank you.
quick question on the end. now I have values:
"feature_1" => "1"
"feature_4" => "4"
"feature_5" => "5"
"feature_8" => "8"
"feature_39" => "39"
"feature_42" => "42"
"feature_65" => "65"
"feature_68" => "68"
how you checking how many max values you have ? I am not sure is it good idea to run for when I don't know how many values we have...
Give this a whirl and let us know.
<ul class="optional-features-list">
@foreach ($features as $feature)
@if ($feature->category_name == 'Interior Features')
@if (isset(old('feature.' . $feature->id'))
<li class="checkbox">
<label><input id="features" name="feature[{{$feature->id}}]" type="checkbox" checked>{{$feature->feature_name}}</label>
</li>
@else
<li class="checkbox">
<label><input id="features" name="feature[{{$feature->id}}]" type="checkbox">{{$feature->feature_name}}</label>
</li>
@endif
@endif
@endforeach
</ul>
@willvincent
Hmm.. I've tried and it didn't work, maybe blade supports it now. Or do you have it on separate lines?
It's all in one line. But yours looks nicer and probably is more 'future-consistent' than mine ;)
This is an old post, but here is another example (using the same name for all the checkboxes, with different values):
@foreach ($categories as $category)
<div class="col-md-4 col-sm-6">
<input type="checkbox" name="categories[]" value="{{ $category->id }}" {{ ( is_array(old('categories')) && in_array($category->id, old('categories')) ) ? 'checked ' : '' }}/> {{ $category->name }}
</div>
@endforeach
As the new directive @checked mentiond In Laravel 9.x ; this worked for me too
<input
type="checkbox"
name="tags[]"
@checked(in_array($tag->id, old('tags', [])))
value="{{ $tag->id }}"
>
Please sign in or create an account to participate in this conversation.