yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Compare validator with date #10832

Closed calcio closed 8 years ago

calcio commented 8 years ago

I tried use [[yii\validators\CompareValidator|compare]] to compare 2 dates but always show error like date1 must be greater than "date2".

date1 = 01/03/2016 date2 = 04/03/2016.

04/03/2016 is greater than 01/03/2016

OBS: date in BR forma

SilverFire commented 8 years ago

CompareValidator is not designed to compare dates. Use FilterValidator with the custom validation rule insted (SO question) or refer to a 3rd-party extensions like this one.

yii-bot commented 8 years ago

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem. If the result of the discussion turns out that there really is a bug in the framework, feel free to come back and provide information on how to reproduce the issue. This issue will be closed for now.

Faryshta commented 8 years ago

i think a CompareDateValidator can be useful too.

cebe commented 8 years ago

comparing dates can work when using compare validator together with date validator. Reopening this for docs.

Faryshta commented 8 years ago

@cebe can you explain a bit better what you mean? So i can help with the docs

cebe commented 8 years ago

basic idea is to have 3 validators, using timestampAttribute in date validator to overwrite input value with machine readable format:


['fromDate', 'date', 'timestampAttribute' => 'fromDate'], 
['toDate', 'date', 'timestampAttribute' => 'toDate'], 
['fromDate', 'compare', 'compareAttribute' => 'toDate', 'operator' => '<', 'enableClientValidation' => false], 

disable client validation because date validator does not work on client, this needs to be done via ajax validation.

Thanks! and let me know if you need further info.

Faryshta commented 8 years ago

@cebe I honestly think that the lack of js validation is a deal breaker. At least to me as soon as i see that js validation is disabled i would check other solutions.

would you like me to write a compare validation for dates using moment.js for js validation?

cebe commented 8 years ago
  1. whats wrong with using ajax validation?
  2. the thing about client validation is, that is has to work exactly the same as server validation if it is provided, I do not think it is easy if possible at all to achive that for date validator.
Faryshta commented 8 years ago

1) its not always enabled and not always enforceable, such as when handling files with the form or using js widgets.

2) yes i know, thats why i am proposing to use moment.js

cebe commented 8 years ago

how do you ensure it does the same as server validation?`does it use intl/icu, or at least the same data from cldr?

Faryshta commented 8 years ago

momento.js has i18n support and timezone support

http://momentjs.com/timezone/

pasing the configuration to moment.js would be pretty much like when you pass it to the datepicker widget.

But I think the only relevant part is http://momentjs.com/docs/#/parsing/ we will use it for comparing data, for example

['publicationDate', 'compareDate' 'operator' => '>', 'value' => '2015-01-01']

For that there is no i18n required, just parsing the user input date and comparing to the static date. Or

['dateFinish', 'compareDate' 'operator' => '>', 'targetAttribute' => 'dateStart']

in which case only needs to parse it to know which of the user inputs is determined to be the highest.

The only issue where we might need the time zone is in the case of using now() or similar in which case already datepicker has support for it without yii2 passing the icu or cldr configuration to the js.

cebe commented 8 years ago

what you show are the simple cases but date validator can work with dates like May 25, 2016 and even 25. май 2016. how to ensure client validation handles it the same way as server?

Faryshta commented 8 years ago

@cebe in the link i provided there is an example of that

moment('2012 juillet', 'YYYY MMM', 'fr');
moment('2012 July',    'YYYY MMM', 'en');
cebe commented 8 years ago

sounds cool to me, but that would mean the core would depend on another js library. We could create a date validation extension that enhances core date validator with this functinoality.