1

First, I've successfully implemented a Multiple Select box with option groups based on providing an providing a group of options structured like this:

$options = [
   'Group 1' => [
      'Value 1' => 'Label 1',
      'Value 2' => 'Label 2'
   ],
   'Group 2' => [
      'Value 3' => 'Label 3'
   ]
];
echo $this->Form->select('field', $options);

This is straight from the Cookbook at: http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-select-pickers

It works great, and makes a select box exactly as it should. My issue is that I would like to change the display to be multiple checkboxes. In order to do that, I've switched the code to:

echo $this->Form->input('field', [
        'multiple' => 'checkbox',
        'options' => $options
    ]);

When I do this, the display ends up being a single checkbox, with all the options listed out next to it.

In searching stackoverflow, I found the following: How to create multiple checkboxes grouped by fieldsets in Cakephp 3

Most seem to indicate that the functionality is not included in Cake, and that you need to build it on your own. There is one comment on the initial question that references the cookbook and that it specifically states:

If you would like to generate a select with optgroups, just pass data in hierarchical format. This works on multiple checkboxes and radio buttons too, but instead of optgroups wraps elements in fieldsets:

No one seems to address the comment on that question. My question is really simple. Does CakePHP 3 allow for multiple checkboxes created as outlined in the documentation, or is the documentation incorrect and this functionality isn't included in the core? If the answer is that the functionality is included in the core, what's the trick to getting it to work?

Thanks!

Community
  • 1
  • 1
jtesta
  • 183
  • 3
  • 15
  • Last time I looked into it, the documentation was wrong here. I hope that at some point someone implements the functionality it describes. – Greg Schmidt Feb 26 '16 at 16:16
  • Greg, thanks for your feedback. I implemented the answer found in the linked question successfully, but didn't go as far as making my own widget as I only needed it in one spot. If I do make a widget, I'll be sure to share it here. That said, it would be nice to have the functionality described in the book included in the core. – jtesta Feb 27 '16 at 18:26

1 Answers1

1

in that multi select checkbox

<?= $this->Form->select('input_name',$checkboxarray, array('selected' =>$send_checkbox_select,'multiple' => 'checkbox')); ?>

$checkboxarray=[
  'Value 1' => 'Label 1',
  'Value 2' => 'Label 2'
`],
  • Your solution would work for creating a multiple select in checkbox form, but it does not answer the question. The issue is when including your options in a hierarchical format with the goal of getting grouped checkboxes. Your solution only includes one level of options, not a grouping level and then an options level as outlined in the original question. – jtesta Feb 26 '16 at 15:38