Yii Framework Forum: Yii Client Side Date Comparison Checks Only Year - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Client Side Date Comparison Checks Only Year Rate Topic: -----

#1 User is offline   Greeshma 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 04-March 13

Posted 12 March 2013 - 06:30 AM

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:

<div class="form">
    <?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'payment-form',
	'enableAjaxValidation'=>false,
	'enableClientValidation' => true,
	'clientOptions'=> array('validateOnSubmit'=>true),
    )); ?>

       <!--other fields -->

        <div class="row">
		<?php echo $form->labelEx($model,'opening_date'); ?>
		<?php echo $form->textField($model,'opening_date'); ?>
		<?php echo $form->error($model,'opening_date'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'closing_date'); ?>
		<?php echo $form->textField($model,'closing_date'); ?>
		<?php echo $form->error($model,'closing_date'); ?>
	</div>

        <div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

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

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


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.
0

#2 User is offline   nakovn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 18-July 12

Posted 21 March 2013 - 04:58 AM

I have the same issue, anybody help me
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,527
  • Joined: 16-February 11
  • Location:Japan

Posted 22 March 2013 - 07:45 AM

Hi Greeshma and nakovn,

Please see the source code of CCompareValidator.clientValidateAttribute()
http://www.yiiframew...ttribute-detail
(Click on "show" link to see the source code.)

Quote

...
        case '>':
            if($message===null)
                $message=Yii::t('yii','{attribute} must be greater than "{compareValue}".');
            $condition='parseFloat(value)<=parseFloat('.$compareValue.')';
            break;
...


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.yiiframew...ttribute-detail

Quote

...
        case '>':
            if($value<=$compareValue)
                $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than "{compareValue}".');
            break;
...



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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users