Yii Framework Forum: CHtml::activeCheckBox - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

CHtml::activeCheckBox Rate Topic: -----

#1 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 14 January 2009 - 11:46 PM

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?
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 January 2009 - 06:36 AM

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

#3 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 15 January 2009 - 06:57 PM

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

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

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,901
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 January 2009 - 07:02 PM

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

#5 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 15 January 2009 - 07:53 PM

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..
0

#6 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 10-October 08

Posted 11 May 2009 - 01:58 PM

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?
0

#7 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 10-October 08

Posted 11 May 2009 - 11:14 PM

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....'), 
  );
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users