How to create multiple checkboxes grouped by fieldsets in Cakephp 3

限于喜欢 提交于 2019-12-14 03:47:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!