Beforevalidate Not Working For Me

hi every body

i’ve created a crud using gii

i add some check boxes in the form to control showing or hiding textfields

i want to implement this logic rule "when you check check box and textfield appears this textfield is required"

so i use beforeValidate() on my cmodel like the following

public function beforeValidate()


        if ($this->recycle) {//this is the checkbox


                'class' => 'CRequiredValidator',

                'attributes' => array('recycle_options'),//this is the textbox



        return parent::beforeValidate();


in my controller created automatically using gii

public function actionCreate()


		$model=new MembershipItemCycles;

		// Uncomment the following line if AJAX validation is needed












i tested that with an empty textfield with checkbox checked but nothing occurs and no errors and fields saved to my database

what error i’ve done ?

Just a thought: you can set the textbox validation rule as usual but add scenario option (for example, ‘on’ => ‘recycle’), and then switch model validation scenario if the checkbox is checked.

Dear Friend

Check whether the following is helpful. (Not tested the code in my localhost. Kindly bear with me).

public function beforeValidate()


        if ($this->recycle) {//this is the checkbox



        return parent::beforeValidate();


non of the above solution works for me ?

is there is any other idea?

Dear Friend

Following code works!.

If user selects married status. He has to enter his wife name.

If he puts wife name without selecting married status. His wife name silently discarded.



class TestForm extends CFormModel


public $name;

public $married;

public $wifeName;

public function rules()


		return array(

			array('name', 'required'),

			array('married', 'boolean'),

			array('wifeName', 'safe'),





	public function beforeValidate()


        if ($this->married) {



         if (!$this->married) {



        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,'wifeName'); ?>

		<?php echo $form->textField($model,'wifeName'); ?>

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


	<div class="row buttons">

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


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

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


public function actionValidate(){

	$model=new TestForm;











thanks for replay

i’ve noticed somthing

your model class extends CFormModel not CActiveRecord which i extends

is this why my code doesnot works?

finally i understand why my code does not work will

recycleshould be assigned to a rule even if it will be assigned to safe