CHtml Checkbox List

Using CHtml class we are creating checkBoxList in yii. This post will help you to understand about how to create checkBoxList in different method, How to assign value, how to get checkBoxList value in controller, checkBoxList value from database, checkBoxList validation in model and checkbox list style.

checkBoxList

Using below code, we can create checkbox list in different way. And i added checkbox list value from database and assigned static array.

activeCheckBoxList With CHtml

CHtml activeCheckBoxList generates a checkbox list for a model attribute.

<?php 
echo CHtml::activeCheckBoxList(
            $model,
            'name',
            array('1'=>'One','2'=>'Two')
        ); 
//OR 
$type_list=CHtml::listData(
            Types::model()->findAll(),
            'id',
            'type'
            );
echo CHtml::activeCheckBoxList($model,'name',$type_list); 
?>

checkBoxList With Form


<?php 
echo $form->checkBoxList(
            $model,
            'name',
            array('1'=>'One','2'=>'Two')
        ); 
//OR
$type_list=CHtml::listData(
            Types::model()->findAll(),
            'id',
            'type'
        );
echo $form->checkBoxList($model,'name',$type_list); 
?>

checkBoxList With CHtml (Without Model Name)


<?php 
//syntax:CHtml::checkBoxList(string name, default_select_by_array_keys,array_value());
<?php 
echo CHtml::checkBoxList(
            'Typelist[name]',
            $selected_Array=array(),
            array('1'=>'One','2'=>'Two')
        ); 
//OR
$type_list=CHtml::listData(
                Types::model()->findAll(),
			    'id',
			    'type'
            );
echo CHtml::checkBoxList(
            'Typelist[name]',
            $selected_Array=array(),
            $type_list
        );
?> 
?>

Output HTML


<span id="Typelist_name">
    <input value="1" id="Typelist_name_0" type="checkbox" name="Typelist[name][]">
    <label for="Typelist_name_0">One</label>br<
    <input value="2" id="Typelist_name_1" type="checkbox" name="Typelist[name][]">
    <label for="Typelist_name_1">Two</label>
</span>

checkBoxList Validation In Model

Add the validation to checkBoxList value in model.

<?php
.....
array('name', 'in','range'=>array(1,2)),
.....
?>

checkBoxList Value In Controller


<?php
.........
$model->attributes=$_POST['Typelist'];
foreach($model->name as $checkbox_id)
{
    echo $checkbox_id;
}
.......
?>

Selected checkbox List Value From Database


//already selected value
$selected_type_list=CHtml::listData(
                Typelist::model()->findAll(),
                'id',
                'type'
                );
//all type list
$type_list=CHtml::listData(
                Types::model()->findAll(),
                'id',
                'type'
                );
echo CHtml::checkBoxList(
                'typelist', 
                $selected_type_list,
                $type_list
                );

checkbox List Style

Normally checkbox will display one by one. We can change this style using labelOptions. See below code


<?php
echo $form->checkBoxList(
                $model,
                'type',
                array('1'=>'Admin','2'=>'User'),
                array(
                    'template'=>'{input}{label}',
                    'separator'=>'',
                    'labelOptions'=>array(
                        'style'=>'
                        padding-left:13px;
                        width: 60px;
                        float: left;'
                        ),
                    'style'=>'float:left;',
                )                              
            );
?>
<div style="clear: both;"></div>

  • danilo di moia

    very nice & clear, thank you!

  • very nice and helpful

  • Resly

    Helpful. Thank You!

  • Budi Santoso

    Great sample! Thanks for sharing…

  • Ricardo Magaña

    I write the code to fill a cheklist from a table but only I get 1 record

    findAll(“estado = 1 and esjefe = 1”),’id’,’NombreCorto’); ?>
    checkBoxList($losCc,’Empleado_id’,$type_list); ?>

    Help

    • m bala

      Hi Ricardo,
      There is no more logic here. Just we fetch all the record using findAll() method. after We will convert to array using listData() and finally have to assign that array to checkBoxList().
      Please check your findAll() condition.

  • Ali Eren

    Hi .
    I want to create from chtml without model. I want to only 2 selection on checkboxlist but i dont apply limit for checkboxlist . How can do it?
    ‘One’,’2’=>’Two’,’3’=>’uc’,’4’=>’dort’);

    echo CHtml::checkBoxList(‘list_name’, $selected_ids, $data_list,
    array(
    ‘onclick’=>’javascript:chkcontrol();’,)
    );
    ?>