Reputation: 15464
I have a loop of checkboxes with multiple attributes. One content may have many attributes, so user can check more than one attribute.
If some validation errors occurs in this form I need to pre-fetch already checked attribute.
@foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4" class='icheck-me'
name="attribute[<?php echo $entry->id; ?>]"
data-skin="square"
data-color="blue"
value="{{$entry->id}}"
<?php
if(Input::old('attribute['.$entry->id.']')== $entry->id) {
echo 'checked="checked"';
}
?>
/>
<label class='inline' for="cat4">
<strong>{{$entry->name}}</strong>
</label>
</div>
@endforeach
I tried above but no luck.. any ideas?
Upvotes: 1
Views: 2119
Reputation: 14212
A slightly more catch-all answer to this would be to do an array search rather than just checking the current index. As such, I see the code looking like this:
@foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4_{{{ $entry->id }}}" class="icheck-me"
name="attribute[]"
data-skin="square"
data-color="blue"
value="{{{ $entry->id }}}"
@if (in_array($entry->id, Input::old('attribute')))
checked
@endif
/>
<label class="inline" for="cat4_{{{ $entry->id }}}">
<strong>{{{ $entry->name }}}</strong>
</label>
</div>
@endforeach
So first off I've made the code more Blade-compliant:
{{{
and }}}
rather than the previous mix of <?php echo ___; ?>
and {{
/}}
(I went for {{{
rather than {{
as it's not HTML being echoed and it's better to be safer)<?php if () {
is now a Blade @if ()
The name
attribute is now just a standard array (doesn't include indices, as it doesn't need to), however the id
attribute previously gave all checkboxes the same ID. While browsers will let you do this, it's technically illegal HTML, so I've appended the entry ID to each checkbox.
Finally, the if condition no longer checks that the value of the input with the current index matches the current entry's ID, but instead searches to see if the current entry's ID is anywhere in the returned array. This protects against the possibility of your entries being returned in a different order to the previous time they were on the page.
We now don't have a reliance on the $ii variable, so it can be removed too.
Caveats:
In doing this I've made the code a little nicer, but the code is no longer identical. Without knowing exactly why you use the $ii variable in order to provide keys to your attribute
array I can't say that using my code will work correctly. However, assuming it was just there to help with this old input thing, then it's fine.
Also, my change of {{
to {{{
may have consequence I don't know about. I'd have thought for the $entry->id
stuff it'd be fine, but maybe $entry->name
in the <label>
need to be unescaped HTML for a reason. Feel free to change this back.
Upvotes: 3
Reputation: 7578
From Laravel docs on Requests:
"When working on forms with "array" inputs, you may use dot notation to access the arrays:"
$input = Input::get('products.0.name');
So you should be able to do this fpr Input::old()
as well:
<?php
if(Input::old('attribute.' . $ii) == $entry->id) {
echo 'checked="checked"';
}
?>
Upvotes: 4