How to pre select radioButtonListRow and checkboxListRow with Yii 2


Looking at the code in Yii for radioButtonListRow and checkboxListRow it’s not apparent how to preselect these form elements with previously selected data.

<?php echo $form->checkboxListRow($model, 'id', CHtml::listData(models\web_data\Program::model()->findAll(array('order' => 'name')), 'id', 'name')); ?>

The solution is to set the model’s property to your selected values in the controller. You can use a scalar value or an array of values. For the above line of code, we can use the below –

$model->id = array(2,3);

or

$model->id = 2;

Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “How to pre select radioButtonListRow and checkboxListRow with Yii

  • Kabir

    Hi Larry, I loved the article. To be hoesnt, there are a lot of people saying a lot of good things about Yii but not enough external resource, I mean good ones other than the website and thank God your bunch of articles. Codeigniter’s user guide is a lot better.Since there are only 2 books on Yii, have you thought of writing one? I have 2 of your PHP & MySQL books and I’ve glanced through some of the rest and you explain things really well and your examples are relevant. I’m actually finding it a little harder to pick up Yii. It’ll be a little helpful to have a good reference. No disrespect but I heard the current book is not as good as it should be.I’ve blabl-ed a lot I apologize, I just wanted to thank you for the articles.