Conditional Validation

hello everyone,

Im still new to YII and is currently working on my first project.

Ive been wondering about how on form conditional validations can be done, for eg:-

In my view I have two time pickers like:-




<div class="row" id="start_time">

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

                 <?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

                        $this->widget('CJuiDateTimePicker',array(

                        'model'=>$model, 

                        'attribute'=>'startTime', 

                        'mode'=>'time' ,

                            'options'=>array("timeFormat"=>'hh:mm:ss'), 

                             'language' => ''

                        ));

                ?>

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

	</div>

        

        <div class="row" id="msg1">

		

	</div>


	<div class="row" id="end_time">

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

                <?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

                        $this->widget('CJuiDateTimePicker',array(

                        'model'=>$model, 

                        'attribute'=>'endTime', 

                        'mode'=>'time' ,

                            'options'=>array("timeFormat"=>'hh:mm:ss'), 

                             'language' => ''

                        ));

                ?>

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

	</div>



I need to check if the start time is always greater than end time, I know this is really basic but how can I achieve this?

Thanks a lot for your time, really appreciate it! :)

Cheerz!

Got it done using a custom validator…!

Cheerz! :)

Hope i didnt waste anyone’s time :)

You can do it by overriding beforeValidate method in the model.




public function beforeValidate()

{

  if($this->startTime < $this->endTime)

   $this->addError('startTime','Start Time must be greater than end time');




   return parent::beforeValidate();

}



@Chandrakanta - thanks a lot for the reply, yeah thats a good method…! :)

Cheerz!