4

I'm having a problem following this documentation :

Cakephp3 Cookbook - Form - Creating Select Pickers I tried the 'multiple checkboxes' part :

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

but the output was an error like this :

Notice (8): Array to string conversion [CORE/src/View/StringTemplate.php, line 238]

it's like telling me that the value of the array should be a string instead of an Array, but is there anyway to make this work ? Please can anyone help me solve this problem ?

Kadim Ahmed
  • 55
  • 1
  • 6
  • 1
    I don't think it is (currently) possible to groups with multiple checkbox, the groups only work with standard select. – Holt Sep 05 '15 at 11:14
  • is that so, too bad then, thanks @Holt – Kadim Ahmed Sep 05 '15 at 13:47
  • The documentation currently says "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". I tried this with CakePHP 3.1, and it didn't generate an error, but it also didn't seem to generate expected output. Using the `['multiple' => 'checkbox']` option on the select call resulted in too many checkboxes being created, and using `['options' => $options, 'multiple' => 'checkbox']` on an input call resulted in too few... I didn't test it beyond that. – Greg Schmidt Oct 22 '15 at 17:23

1 Answers1

4

As @Holt already mentioned in the comments, what you're doing there is simply not supported. If you think this might be useful, you can suggest it as an enhancement over at GitHub.

What you can do for now, is either building it half-way manually, like for example

foreach ($options as $group => $groupOptions) {
    $legend = $this->Html->tag('legend', $group);
    $checkboxes = $this->Form->select($group, $groupOptions, [
        'name' => 'field',
        'multiple' => 'checkbox'
    ]);
    echo $this->Html->tag('fieldset', $legend . $checkboxes);
}

or, for better reusability, create a custom widget that can handle such structures.

See Cookbook > View > Helpers > Form > Adding Custom Widgets

ndm
  • 59,784
  • 9
  • 71
  • 110