Verifying Date Range

I am trying to validate the input from two input fields.

views/vacation/create:

[spoiler]





<?php

/* @var $this VacationController */

/* @var $model Vacation */


if(Yii::app()->user->isAdmin()) {

    $this->breadcrumbs=array(

        'Vacations'=>array('index'),

        'Create',

    );

    $this->menu=array(

        array('label'=>'Manage Vacation', 'url'=>array('admin')),

    );

} else {

    $this->menu=array(

        array('label'=>'List Vacation', 'url'=>array('index')),

    );

}            

?>


<h1>Create Vacation</h1>


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>

[/spoiler]

views/vacation/_form:

[spoiler]




<?php

/* @var $this VacationController */

/* @var $model Vacation */

/* @var $form CActiveForm */

?>


<div class="form">


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

	'id'=>'vacation-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,'startdate'); ?>

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

                <?php

                Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

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

                    'model'=>$model, //Model object

                    'language' => 'de',

                    'attribute'=>'startdate', //attribute name

                    'mode'=>'date', //use "time","date" or "datetime" (default)

                    'options'=>array (

                        'dateFormat'=>'yy.mm.dd',

                    ),

                ));

                ?>

	</div>

        

        <div class="row">

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

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

                <?php

                Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

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

                    'model'=>$model, //Model object

                    'language' => 'de',

                    'attribute'=>'stopdate', //attribute name

                    'mode'=>'date', //use "time","date" or "datetime" (default)

                    'options'=>array (

                        'dateFormat'=>'yy.mm.dd',

                    ),

                ));

                ?>

	</div>


	<div class="row">

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

		<?php echo $form->dropDownList($model,'User_ID', CHtml::listData(User::model()->findAll(), 'ID', 'name')); ?>

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

	</div>

        

	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


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


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

[/spoiler]

The startdate should not be earlier than the stopdate.

I know how to write a function which does the logic but I have no idea how I can tell yii to use this function.

Any help would be greatly appreciated.

Like this?


        <div class="row">

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

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

                <?php

                Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

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

                    'model'=>$model, //Model object

                    'language' => 'de',

                    'attribute'=>'startdate', //attribute name

                    'mode'=>'date', //use "time","date" or "datetime" (default)

                    'options'=>array (

                        'dateFormat'=>'yy.mm.dd',

                        'onSelect' => "js:function(selectedDate) {

                            $('#" . CHtml::activeId($model, 'stopdate') . "').datepicker('option', 'minDate', selectedDate);

                        }",

                    ),

                ));

                ?>

        </div>

        

        <div class="row">

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

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

                <?php

                Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

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

                    'model'=>$model, //Model object

                    'language' => 'de',

                    'attribute'=>'stopdate', //attribute name

                    'mode'=>'date', //use "time","date" or "datetime" (default)

                    'options'=>array (

                        'dateFormat'=>'yy.mm.dd',

                        'onSelect' => "js:function(selectedDate) {

                            $('#" . CHtml::activeId($model, 'startdate') . "').datepicker('option', 'maxDate', selectedDate);

                        }",

                    ),

                ));

                ?>

        </div>

You can comapre dates in BeforeSave(),& also do as you want.

you don’t have to write a function it’s a simple task by using rule method of model

in your model do the following:




public function rules()

{

   return array(

			

   array('startdate', 'compare', 'compareAttribute'=>'stopdate', 'operator'=>'<'),

   array('stopdate', 'compare', 'compareAttribute'=>'startdate', 'operator'=>'>'),

            

      );

}



Thank you very much, this was exactly what I was looking for.