CHtml::activeCheckBox

CHtml::activeCheckBox() seems to post '1' when checked, but an empty string ('') when unchecked.  I want a single checkbox for a bunch of attributes (such as email_publicly_visible) which have boolean values.  When the checkbox is unchecked, I get a mysql error because it can't store null into the column.

Should CHtml::activeCheckBox() actually post '0' when unchecked?  Is that a bug?

Should an argument be added to it that defines what the value is when unchecked?

Yes, I think adding an option to specify unchecked value is good. Could you create a ticket for that? Thanks!

Another question:  What's the best way to validate checkboxes?

May I suggest to add another type to CTypeValidator that is "boolean"?

Not sure if this is needed. There are several ways to validate a boolean (e.g. CRangeValidator, CNumberValidator). I prefer to using CRangeValidator.

Ok.  Yes, it is definitely not needed, but I think it would be less typing.  And I don't see why the type validator should not support it anyways, it is a ''data type" after all.

I'll use the range validator however…

I want to validate checkBox to know user accept terms and conditions. But the validation is not works.

I did like this:

In model



public function rules()


	{


		return array(


			array('terms', 'required'),  


			array('terms', 'length', 'min'=>1,


				'message'=>'You must accept to continue....'),  


		);


	}


in views:



<?php echo CHtml::activeLabelEx($confirm,'terms'); ?>


<?php echo CHtml::activeCheckBox($confirm,'terms'); ?>


<?php echo CHtml::error($confirm,'terms'); ?>


How to validate this checBox?

Sorry, it should be like this:



public function rules()


{


   return array(


       array('terms', 'required'),  


       array('terms', 'CRangeValidator', 'range'=>array(1),


       'message'=>'You must accept to continue....'),  


   );


}