Pre-checked Check Box stays checked

Hi,

I have a simple checkbox in my view:


<?php echo $form->checkBox($model,'nameOfCheckBox', array('value' => 'yes', 'uncheckValue' => 'no', 'checked' => true)); ?>

The checkbox is preselected once the form view is opened. However, if I deselect it and the form doesn’t validate the checkbox returns selected again.

What am I missing here that the checkbox won’t get automatically be selected if the user chooses to deselect it?!

Thanks a lot,

jrn

It’s because you set the “checked” attribute to true, and it will override any nameOfCheckBox value. Instead set nameOfCheckBox value inside a model class (or somewhere outside), so after a massive assignment this value can change:




class MyModel extends CActiveRecord

{

    public $nameOfCheckBox = 'yes'; // or true, I don't actually know which one will work there


    // ...

}



Thank you!! :)

I ended up doing exactly what you suggested. It works how it is supposed to but I don’t feel like “yii” ;)