How to make checkbox check by default in yii while using single form for create and update

formsyii

I am using single form for creating and updating a form.I need checkbox checked by default in the following form

<div class="row" style='float:left;;margin-left:5px'>
        <?php echo '<span for="label" style="margin-bottom:5px;font-size: 0.9em;font-weight: bold;">Label</span><br />'; ?>
        <?php echo $form->checkBox($model,'label_name',array('value'=>1,'uncheckValue'=>0,'checked'=>'checked','style'=>'margin-top:7px;')); ?>
        <?php echo $form->error($model,'label_name'); ?>
    </div>

i am using above code to achieve the same purpose i am not getting the result as expected. While updating the form it shows checked even though it was unchecked

Best Solution

i got solution i worked with code itself please have a look

<div class="row" style='float:left;;margin-left:5px'>
        <?php echo '<span for="label" style="margin-bottom:5px;font-size: 0.9em;font-weight: bold;">Label</span><br />'; ?>
        <?php echo $form->checkBox($model,'label_name',array('value'=>1,'uncheckValue'=>0,'checked'=>($model->id=="")?true:$model->label_name),'style'=>'margin-top:7px;')); ?>
        <?php echo $form->error($model,'label_name'); ?>
    </div
Related Question