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


            $this->validatorList->add(Yii::createComponent(array(

                '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

		 //$this->performAjaxValidation($model);


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

		{

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

			if($model->save())

				$this->redirect(array('view','id'=>$model->cycle_id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}



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


            $this->validatorList->add(CValidator::createValidator('required',this,'recycle_options',array()));

        }


        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.

MODEL:TestForm




<?php

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


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

        }

         if (!$this->married) {


            $this->getValidatorList()->add(CValidator::createValidator('default',$this,'wifeName',array('value'=>'','setOnEmpty'=>FALSE)));

        }

        return  parent::beforeValidate();

    }

}


?>






VIEW:validate.php




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

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

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

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

	}



Regards.

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