How To Set Checkbox Checked If Value I 1?

what parameters to add in my code?




$form->checkBox($model,'ATTRIBNAME',array("id"=>"mybox"));?> 



i wanna display the checkbox as checked if the value from the db is 1 and unchecked if the value it got from the db is 0 ?

I don’t know if it depends on the db you are using. In mysql, I use boolean type and mysql stores it as tinyint(1) I don’t have to add any code to monitor this. It is done automatically by Yii.

Dear Friend

Kindly check whether the following is helpful.

Override the afterFind method in your model.




public function afterFind()

{

	if($this->atribute==1) $this->attribute=true;

	if($this->attribute==0) $this->attribute=false;

	return parent::afterFind();

}



there is activeCheckbox as well make use of that if the value from the database is 1 it will be checked automatically if its 0 it would not be checked




$form->activeCheckBox($model,'ATTRIBNAME',array("id"=>"mybox"));

If you have done things correctly, the code that you have used should work automatically.

Are you testing this on INSERT or UPDATE?

On INSERT (new record) it will use the ‘default’ value that you set when creating the column in the database table, so if that is set to 0 (zero) or there is no default, the box will be unchecked.

On update, the box will also be checked depending upon the value of the database field.

Why not echo the value in your page somewhere to see if the value is what you expect?


echo $model->ATTRIBNAME;