0

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.

3
  • Try using $this->Form->checkbox('tag_id.', array( 'value'=> $tag_id)) Commented Jan 19, 2018 at 11:51
  • That works. But is that the correct Cake way of doing it? The output seems correct as I think data[GroupTag][tag_id][] is right for what I'm trying to do - loop through multiple checkboxes with the name tag_id[]? Commented Jan 19, 2018 at 11:57
  • Yes, this is the correct way Commented Jan 19, 2018 at 12:01

2 Answers 2

1

Try this

$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
Sign up to request clarification or add additional context in comments.

Comments

-1

You can also do this:

$this->Form->input('GroupTag.tag_id', [
    'type' => 'select',
    'multiple' => 'checkbox',
    'options' => $tag_id
]);

FormHelper::select(string $fieldName, array $options, array $attributes)

Example:

$options = array(
    'Value 1' => 'Label 1',
    'Value 2' => 'Label 2'
);

echo $this->Form->select('Model.field', $options, array(
    'multiple' => 'checkbox'
));

Output:

<div class="input select">
   <label for="ModelField">Field</label>
   <input name="data[Model][field]" value="" id="ModelField"
    type="hidden">
   <div class="checkbox">
      <input name="data[Model][field][]" value="Value 1"
        id="ModelField1" type="checkbox">
      <label for="ModelField1">Label 1</label>
   </div>
   <div class="checkbox">
      <input name="data[Model][field][]" value="Value 2"
        id="ModelField2" type="checkbox">
      <label for="ModelField2">Label 2</label>
   </div>
</div>

4 Comments

That doesn't work inside a loop. If outputs every single checkbox on each iteration of the loop. For example if the loop executes 10 times and there are 5 checkboxes, you get 50 inputs (the same ones on each line).
@Andy, no need to loop.. just use this one time.
Re-read the question! Specifically the bit which says "output the checkboxes in between Bootstrap markup". The markup has to be output each time the loop executes, hence the need for the loop in the first place.
You can customize the output template - book.cakephp.org/2.0/en/core-libraries/helpers/…. $options['before'], $options['between'], $options['separator'], and $options['after'] Use these keys if you need to inject some markup inside the output of the input() method. The ordering of the HTML generated by FormHelper is controllable as well. The ‘format’ options supports an array of strings describing the template you would like said element to follow. The supported array keys are: array('before', 'input', 'between', 'label', 'after','error').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.