如何创建多个复选框按字段集分组在Cakephp 3 [英] How to create multiple checkboxes grouped by fieldsets in Cakephp 3

查看:132
本文介绍了如何创建多个复选框按字段集分组在Cakephp 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下列文件之后发生问题:

I'm having a problem following this documentation :

Cakephp3 Cookbook - 表单 - 创建选择选择器
我尝试了多个复选框部分:

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']);

但输出的错误如下:

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 ?

推荐答案

由于@Holt在评论中已经提到,简单不支持。如果您认为这可能有用,您可以 将其建议为增强功能

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.

查看 Cookbook> View> Helpers> Form>添加自定义小部件

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

这篇关于如何创建多个复选框按字段集分组在Cakephp 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆