Cakephp 3无法标记所选的多个复选框 [英] Cakephp 3 Unable to mark multiple checkboxes selected

查看:171
本文介绍了Cakephp 3无法标记所选的多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编辑视图文件有一组复选框。我已经设法从数据库中检索所选的复选框值,并希望将它们标记为在视图文件中选择。

My Edit view file has a set of checkboxes. I have managed to retrieve the selected checkbox values from the database, and want to mark these as selected in the view file.

编辑视图文件

 <div class="col-md-12">
    <?= $this->Form->label('category','Pick Categories');?>
    <?= $this->Form->select('category', $options,['multiple'=>'checkbox', 'required'=>'false', 'label'=>'Category','class'=>'col-md-12','selected'=>$catSel]); ?>
</div>

$ options是

$options is

$options = ['A'=>'Val1',
            'B'=>'Val2',
            'C'=>'Val3',
            'D'=>'Val4',
            'E'=>'Val5'];

$ catSel已在我的控制器中设置,并返回正确的值。我用print_r()检查,如下所示:

$catSel has been set in my controller and is returning the correct values. I checked with print_r(), shown below:

Array ( [0] => 1 [1] => 3 )

我也尝试直接输入所选值

I have also tried directly entering the selected value

<?= $this->Form->select('shop_category', $options,['multiple'=>'checkbox', 'required'=>'false', 'label'=>'Shop Category','class'=>'col-md-12','selected'=>[1,3]]); ?>

我也尝试过在$ catSel中发送

I have also tried sending in $catSel as

Array ( [0] => A [1] => C )

这些都不工作。不知道为什么。除了将选择设置为选择数组之外,我还没有找到任何解决方案。

None of this is working. Not sure why. I haven't been able to find any solution to this anywhere, apart from the set 'selected' as array of selections. Any help will be appreciated.

推荐答案

该选项未调用 selected val 。看起来像 the Cookbook 是有点过时的,但它在 API文档

The option isn't called selected anymore, but val. Looks like the Cookbook is a little out of date on that one, it's however shown correctly in the API docs.

>必须使用您要选择的选项的实际键,即在您的情况下,您必须使用后面的示例,并传递字符串 A C

这篇关于Cakephp 3无法标记所选的多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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