Closed calcio closed 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.
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.
i think a CompareDateValidator can be useful too.
comparing dates can work when using compare validator together with date validator. Reopening this for docs.
@cebe can you explain a bit better what you mean? So i can help with the docs
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.
@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?
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
how do you ensure it does the same as server validation?`does it use intl/icu, or at least the same data from cldr?
momento.js has i18n support and timezone support
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.
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?
@cebe in the link i provided there is an example of that
moment('2012 juillet', 'YYYY MMM', 'fr');
moment('2012 July', 'YYYY MMM', 'en');
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.
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