Yii的CActiveForm-> checkBoxList中的预选项目 [英] pre-selected items in CActiveForm->checkBoxList in Yii

查看:71
本文介绍了Yii的CActiveForm-> checkBoxList中的预选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组。一个是 $ categories ,它包含从数据库中检索到的所有类别,另一个是 $ preSelectedCategories ,它包含需要在我的数据库列表中预先选择的类别表格已加载。我尝试这样做:

I have two arrays. One is $categories which contains all categories retrieved from my db and the other is $preSelectedCategories which contains the categories that needs to be pre-selected in my check box list when my form is loaded. I tried to do this:

<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->checkBoxList($model, 'category_id', $categories, $preSelectedCategories, array('multiple'=>true)); ?>
<?php echo $form->error($model,'category_id'); ?>

但我没有成功。谁能帮我解决这个问题?谢谢!

But I did not succeed. Could anyone help me solve this problem? Thanks!

编辑:我已经知道使用 CHtml :: checkBoxList 可以有所帮助,但是我想在这里使用的是 CActiveForm :: checkBoxList ,因为我使用的是模型来验证复选框列表。

I already knew that using CHtml::checkBoxList could help, but what I want here is to use CActiveForm::checkBoxList because I am using a model to validate the checkbox list.

推荐答案

一个选项是使用 CHtml :: activeName 为输入获取合适的名称,然后像其他人建议的那样将其传递给 CHtml :: checkBoxList

我认为,更合适的另一种选择是在渲染控制器之前,将要预检查的 category_id 添加到控制器中的模型中。表单(仅当它是GET请求时)。然后,您根本不需要修改表单。

Another option that is, in my opinion, more appropriate would be to add those category_ids you want pre-checked to the model in the controller, before rendering the form (only when it's a GET request). Then you wouldn't need to modify the form at all.

这篇关于Yii的CActiveForm-> checkBoxList中的预选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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