How to make Yii checkBoxList selected
Many people find it hard to make the Yii checkBoxList selected on update view, but believe me in fact its very easy and straight forward. This post will help you in showing user’s last selection by showing them a preselected checkbox list.
Need help? You can Email me or Skype chat
I Suppose you have created a MANY_MANY relation as given below.
'authors' => array(self::MANY_MANY, 'Author', 'authorbook(book_id,author_id)'),
'books' => array(self::MANY_MANY, 'Book', 'authorbook(author_id, book_id)'),
Note: authorbook is the table name containing ids
Make a selected checkBoxList
$books = CHtml::listData(Book::model()->findAll(), 'id', 'name'); $selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id')); echo CHtml::checkBoxList('Author[books]', $selected_keys, $books);
checkBoxList function is taking three parameters.
- Field name
- Array of models ids those were previously selected
- Options data
2nd parameter is important here, because it makes the checkBoxList selected.