Checkbox in view.php and _view.php


How can I use a checkbox in update Yii forms (view.php) to reflect a boolean value from a database?



Have you tried to do it?

Can you post your code?

Yes, I can use the checkbox, but for the update and list pages I cannot make it checked if the value from the boolean field in the database is 1/true.

<div class="view">

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('iGrupoID')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::link(CHtml::encode(&#036;data-&gt;iGrupoID), array('view', 'id'=&gt;&#036;data-&gt;iGrupoID)); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('vchNome')); ?&gt;:&lt;/b&gt;

&lt;?php echo CHtml::encode(&#036;data-&gt;vchNome); ?&gt;

&lt;br /&gt;

&lt;b&gt;&lt;?php echo CHtml::encode(&#036;data-&gt;getAttributeLabel('bPedidoExcepcao')); ?&gt;:&lt;/b&gt;

[b]&lt;?php echo CHtml::checkBox(&#036;data-&gt;bPedidoExcepcao); ?&gt;[/b]

&lt;br /&gt;



Check the documentation - http://www.yiiframew…checkBox-detail

You need a second parameter to set the status of the checkbox…

Try with

<?php echo CHtml::checkBox('name',$data->bPedidoExcepcao); ?>

I tried that. I have four records in the database, 2 true and 2 false. With your suggestion I get all the checkboxes checked.

Can I do something like:

<?php echo CHtml::checkBox(‘name’,assertEquals($data->bPedidoExcepcao,1)); ?>


<?php echo CHtml::checkBox(‘name’,assertEquals($data->bPedidoExcepcao,true)); ?>

I think the problem might be with the boolean field type in the MySQL database.

Any thoughts or ideas?

Solution here:

As I suspected it is a MySQL issue.

For google-ing purposes I post this solution for boolean types with gii model generation.