Validation

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);

        $ev->validate($this);

  }

}

and in rules


array('for_rent,for_sale,for_arrenda','validatecheckbox'),

but no validation is trigger.

thanks

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.




<?php


MODEL

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) {


            $this->getValidatorList()->add(CValidator::createValidator('required',$this,'spouseName',array()));

        }

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


            $this->married=true; 

        }

        return  parent::beforeValidate();

    }

}







<div class="form">


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

        'id'=>'test-form',

        'enableAjaxValidation'=>false,

)); ?>


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

        <div class="row rememberMe">

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

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

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

        </div>

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


        <div class="row buttons">

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

        </div>


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


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



CONTROLLER




public function actionValidate(){

        $model=new TestForm;

        if(isset($_POST['TestForm']))

           {

                   $model->attributes=$_POST['TestForm'];

                   

                   $model->validate();

                   }

        

        $this->render('validate',array('model'=>$model));

        }



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

during massive assignment.

Regards.

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(

			

			array('favorite','validateFavorite'),

			

		);

	}


public function validateFavorite($attribute,$params){

		$arr=$this->favorite;

		if(count($arr)<2)

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

		}



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




if(count($this->favorite)>0)



regards.

Hi ,

i got this error :

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

please someone suggest me solution.

thanks in advance