Yii Client Side Date Comparison Checks Only Year

Hi all,

I am working on a form where I have 2 date fields - opening_date and closing_date.

I have added a validation such that closing_date should be greater than opening_date.

Here is the validation on the server side in model:




public function rules()

{

    return array(

        //...other fields rules

        array('opening_date, closing_date','date','format'=>'yyyy-m-d'),			

        array('opening_date','compare','compareValue'=>date('Y-m-d'),'operator'=>'>='),

        array('closing_date','compare','compareAttribute'=>'opening_date','operator'=>'>')

    );

}



And in view:

[html]

<div class="form">

&lt;?php &#036;form=&#036;this-&gt;beginWidget('CActiveForm', array(


'id'=&gt;'payment-form',


'enableAjaxValidation'=&gt;false,


'enableClientValidation' =&gt; true,


'clientOptions'=&gt; array('validateOnSubmit'=&gt;true),


)); ?&gt;





   &#60;&#33;--other fields --&#62;





    &lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'opening_date'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'opening_date'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'opening_date'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'closing_date'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'closing_date'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'closing_date'); ?&gt;


&lt;/div&gt;





    &lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;





&lt;?php &#036;this-&gt;endWidget(); ?&gt;

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

[/html]

Server side date comparison validation is working properly. But the client side date comparison validation is not working properly. I tested it by removing


'enableClientValidation' => true

.

For eg:

If I enter 2013-03-12 in opening_date field and 2013-03-13 in closing_date field, server side validation validates true, which is correct. But the client side validation validates false. And I get this error: Closing Date must be greater than "2013-03-12".

But if I enter 2014-03-13 in closing_date field, client side validation validates true. ie; it compares year, not date.

What am I doing wrong here?

Please help me to solve the issue.

Thanks in advance,

Greeshma.

I have the same issue, anybody help me

Hi Greeshma and nakovn,

Please see the source code of CCompareValidator.clientValidateAttribute()

http://www.yiiframework.com/doc/api/1.1/CCompareValidator#clientValidateAttribute-detail

(Click on "show" link to see the source code.)

As you see, it treats the values as “float”, not “date”. That’s why it just compares “2013” and “2014”, instead of “2013-03-19”.

On the other hand, CCompareValidator.validateAttribute() just compares the values:

http://www.yiiframework.com/doc/api/1.1/CCompareValidator#validateAttribute-detail

So, I think it’s impossible to use the client side validation of CCompareValidator to date fields, at least for the moment.

Try this…


Model rules :

array('closing_date','comparedate','message'=>'Select Proper date'),




Model function : 

public function comparedate($attribute,$params)

{

	if(strtotime($this->opening_date) >= strtotime($this->closing_date))

	{

		$this->addError('closing_date','Closing date must be less than Opening date.');

	}

}



And use datepicker instead of textField for proper date selection.

Thanks Ravi Bhalodiya, but the problem was about the client side validation.

What do you mean by client validation?? Are you not talking about ajax validation???

No.

Some validators support "client validation" that is performed solely with javascript in the client side, without ajax call to the server.

http://www.yiiframework.com/doc/api/1.1/CActiveForm#enableClientValidation-detail

http://www.yiiframework.com/doc/api/1.1/CValidator#clientValidateAttribute-detail