问题
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 ?
回答1:
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
来源:https://stackoverflow.com/questions/32402679/how-to-create-multiple-checkboxes-grouped-by-fieldsets-in-cakephp-3