问题
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!
Edit: 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.
回答1:
One option would be to use CHtml::activeName to get an appropriate name for the input and pass it to CHtml::checkBoxList
, like others have suggested.
Another option that is, in my opinion, more appropriate would be to add those category_id
s 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.
回答2:
You can easily pre-populate a checkBoxList with selected items, it takes an array of selected keys in its second parameter.
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
Please see full example here, on my blog:
http://scriptbaker.com/how-to-make-yii-checkboxlist-selected/
回答3:
CHtml::actviceCheckBoxList
(CActiveForm::checkBoxList that you use is wrapper to it) have such syntax
public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array())
If you want to manually set pre-selected values - you shoul use CHtml::checkBoxList instead
public static function checkBoxList($name,$select,$data,$htmlOptions=array())
Here is the piece of CHtml class reference
* @param string $name name of the check box list. You can use this name to retrieve
* the selected value(s) once the form is submitted.
* @param mixed $select selection of the check boxes. This can be either a string
* for single selection or an array for multiple selections.
* @param array $data value-label pairs used to generate the check box list.
* Note, the values will be automatically HTML-encoded, while the labels will not.
回答4:
<?php
$categories = array(1,2,3);
$preSelectedCategories = array(1=>true,2=>true); // use this way
echo CHtml::checkBoxList('category_id',$preSelectedCategories,$categories);
?>
Try this I have tried, it runs successfully.
回答5:
remove $preSelectedCategories variable. Set $model->category_id to be an array which holds the selected checkbox values.
<?php echo $form->labelEx($model,'category_id'); ?>
<?php
$model->category_id = array('value1','value2');
echo $form->checkBoxList($model, 'category_id', $categories, array('multiple'=>true)); ?>
<?php echo $form->error($model,'category_id'); ?>
You should try this but I did not tested this.
回答6:
Populate your attribute with an array before displaying it on the form.
In the controller:
public function actionUpdate($id) {
$model=$this->loadModel($id);
//For example
$categories = array(0=>'Option One',1=>'Option Two',2=>'Option Three');
$preSelectedCategories = array(1,2);
//Magic here
$model->category_id = $preSelectedCategories;
if(isset($_POST['NameOfModel']) {
//category_id reset with incoming form data...
}
...
$this->render('update',array('model'=>$model,'categories'=>$categories));
}
In the view, in your form:
echo $form->checkBoxList($model,'category_id',$categories);
Untested since I modified it here... adapted from Bootstrap extension's form widgets.
回答7:
Most of yii members having same issue, so here more sweet code with clear explanation.
First you need to find your pre-selected categories like -
$criteria = new CDbCriteria();
$criteria->select = 'category_id as id';
$criteria->condition = 'userid = :userid';
$criteria->params = array(':userid' => Yii::app()->user->id);
//store pre-selected id into variable - $selected_keys
$selected_keys = array_keys(CHtml::listData(MyCategory::model()->findAll($criteria), 'id', 'id'));
Now generate whole category list, like -
$list = CHtml::listData(Categories::model()->findAll(array('order'=>'id')), 'id', 'category_name');
//htmlOptions for class and others elements
$htmlOptions = array('template' => '{input}{label}', 'separator'=>'', 'class'=>'in-checkbox', 'multiple'=>true, 'checked'=>'checked');
View part -
<?php echo $form->labelEx($model, 'Category', array('class'=>'col-md-3 control-label')); ?>
<?php $model->Category = $selected_keys; //assign pre-selected list to Category list
echo $form->checkBoxList($model, 'Category', $list, $htmlOptions); ?>
<?php echo $form->error($model, 'Category'); ?>
Try this, work great..
来源:https://stackoverflow.com/questions/10577231/pre-selected-items-in-cactiveform-checkboxlist-in-yii