I've read CakePHP multiple checkbox array HTML the right way but getting strange results. I have a list of tags (from a Model called Tag
). I want to loop through these and output checkboxes for them in a View.
So I have obtained the Tag
data in my Controller with:
$tags = $this->Tag->find('list', ['order' => ['name' => 'ASC']]);
$this->set('tags',$tags);
When I loop through it in my View I am trying to output the checkboxes in between Bootstrap markup:
<?php echo $this->Form->create('GroupTag'); ?>
<?php foreach ($tags as $tag_id => $tag): ?>
<div class="checkbox">
<label>
<?php echo $this->Form->checkbox('tag_id[]', array( 'value'=> $tag_id)); ?>
<?php echo $tag; ?>
</label>
</div>
<?php endforeach; ?>
I copied the syntax for tag_id[]
from the post I linked to.
But when I inspect the markup it's producing the following as the name
attribute for each <input type="checkbox">
:
data[GroupTag][tag_id[]]
Should this not be
data[GroupTag][tag_id][]
?
The idea is that I have multiple checkboxes with a name
attribute tag_id[]
and then in the Controller I can loop through what has been checked.
Please can someone advise on this as I can't get it working and have looked into examples provided on here/docs.
$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
– Pradeep Singhdata[GroupTag][tag_id][]
is right for what I'm trying to do - loop through multiple checkboxes with the nametag_id[]
? – Andy