how to check if checbox is not checked .

i’m using

public function validatecheckbox($attribute, $params)


  if (!$this->valor_arrenda)


        $ev = CValidator::createValidator('required', $this, $attribute, $params);




and in rules


but no validation is trigger.


Dear led

To access the properties for validation by AR methods, you have to atleast make them initially safe in rules.

Then only they will be massively assigned. Then only we can catch them and apply some rules on them.

One example

We have a form where user has to enter his name.Then there is a boolean field(radio button) for his/her married status.

If he chooses the married status as true, then one has to enter his or her spouse name.

Otherwise errors will be thrown.

If he or she enters his spouseName without choosing the marital status,

then marital status would be assumed true.



class TestForm extends CFormModel


public $name;

public $married;

public $spouseName;

public function rules()


                return array(


                        array('name', 'required'),

                        array('married', 'boolean'),

                        array('spouseName', 'safe'), //this is very important.





        public function beforeValidate()


        if ($this->married) {



         if (!$this->spouseName==null) {



        return  parent::beforeValidate();



<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(



)); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($model); ?>

        <div class="row">

                <?php echo $form->labelEx($model,'name'); ?>

                <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>

                <?php echo $form->error($model,'name'); ?>


        <div class="row rememberMe">

                <?php echo $form->checkBox($model,'married'); ?>

                <?php echo $form->label($model,'married'); ?>

                <?php echo $form->error($model,'married'); ?>


        <div class="row">

                <?php echo $form->labelEx($model,'spouseName'); ?>

                <?php echo $form->textField($model,'spouseName',array('size'=>60,'maxlength'=>64)); ?>

                <?php echo $form->error($model,'spouseName'); ?>


        <div class="row buttons">

                <?php echo CHtml::submitButton('submit'); ?>


<?php $this->endWidget(); ?>

</div><!-- form -->


public function actionValidate(){

        $model=new TestForm;










NOTE: We has to make the rule safe for dependent attribute.Othewise we can not get the value

during massive assignment.


yes , but i can i do that with checkbox?

at least one checkbox must be checked.

Dear led

I understand that you are talking about checkBoxList rather than checkBox.

To ensure that atleast one checkBox gets checked we can use the rule "required".

Consider that we have a checkBoxList for an attribute favorite.

We expect user to select atleast two items.

Then we can do the following.

public function rules()


		return array(






public function validateFavorite($attribute,$params){



		   $this->addError($attribute,'You have to select atleast two items');		


We can use the following code to ensure that one checkBox gets checked.



Hi ,

i got this error :

[color="#FF0000"]Property "CRegularExpressionValidator.0" is not defined.[/color]

please someone suggest me solution.

thanks in advance