Dynamic Minimum Date

I want to set the ‘date of failure’ in a form after ‘date of commissioning’. My code is:


<div class="form">


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

	'id'=>'casesheet-form',

	'enableAjaxValidation'=>false,

)); ?>


....




	<div class="row">

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

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

			'model' => $model,

    			'attribute'=>'dateCommission',

    			'options'=>array(

   		         'showAnim'=>'fold',

   		         'changeMonth' => true,

 			'changeYear' => true,

 			'showButtonPanel' => false, 

			'constrainInput' => false,

                 'maxDate'=>'+0',

   		  		'minDate'=>'-10y',

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

    			),

    			'htmlOptions'=>array(

   		         'style'=>'height:20px;'

    			),

			)); ?>

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

	</div>


...




	<div class="row">

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

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

			'model' => $model,

    			'attribute'=>'dateFailure',

    		// additional javascript options for the date picker plugin

    			'options'=>array(

                 'showAnim'=>'fold',

                 'maxDate'=>'+0',

 				[b]'minDate'=>'#Casesheet_dateCommission',[/b]

 			'showButtonPanel' => false, 

                 'changeMonth' => true,

 			'changeYear' => true,

 			'constrainInput' => false,

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

    			),

    			'htmlOptions'=>array(

   		         'style'=>'height:20px;'

    			),

			)); ?>

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

	</div>




Here I read from the date entered in the ‘Date of Commissioning’ text area and try to insert it as a minimum date in ‘Date of Failure’ field. But the code above doesn’t work. Any help is appreciated!!

You should write a js function, after change of the first date you will set somhow the date on the second.

Another option is to create a CCompareValidator, and enable AjaxValidator. The user can, theoretically, insert an invalid date but they will be alerted and the model will not be saved unless they correct the mistake

CCompareValidator solution is already there at http://www.yiiframework.com/forum/index.php?/topic/3047-2-dates-with-cvalidator/page__p__16917__hl__cvalidator#entry16917. I don’t want to write a js function as JQuery is already built in. I was looking for a simple and elegant solution. Thanks

@thammu - when you post code… please use the code button <> so that your code is more readable…

As for your problem the jquery documentation is a bit vague… but you can use beforeShow() for the second field and there set the minDate to the date chosen on the first field…




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

<?php

   $this->widget('zii.widgets.jui.CJuiDatePicker',array(

  	'model' => $model,

  	'attribute' => 'dateFailure',

  	'options' => array(

  		'showAnim' => 'fold',

  		'maxDate' => '+0',

  		'showButtonPanel' => false,

  		'changeMonth' => true,

  		'changeYear' => true,

  		'constrainInput' => false,

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

  		'beforeShow' => 'js:function(){

          	var selectedDate=$("#'.CHtml::activeId($model,'dtm_racuna').'").datepicker("getDate");

          	$(this).datepicker("option","minDate",selectedDate);

      	}'

  	),

  	'htmlOptions' => array(

  		'style' => 'height:20px;'

  	),

   ));

?>




Thanks a lot! And sorry for the unreadable code.