My final sfDateValidator
April 12, 2007
Update: New download link.
Some time ago i began “fixing” the sfDateValidator. A few hours and some great comments (with great ideas) later, i think that it is safe to say that it’s “fixed”.
What has changed compared to the original sfDateValidator?
- Accept all (strtotime-)date-formats
- New parameters: min and max
- The compare-parameter now accepts all strtotime-formats
- New operator: !=
- UnitTests for Lime
Originally, the sfDateValidator only accepted values in the current i18n-setting language. I really didn’t found this a very good idea since most users are used to enter the date in their commonly used format. (I always use the “dd.mm.YY”-format why should i be forced to enter it in “mm/dd/YY”)?
I changed this so that the validator now accepts all values that the php strtotime-function accepts (You can even use relative values like “tomorrow” or “+1 week“).
If you want the date to be in a specific timeframe, use the new min and max parameters.
Here’s a small example:
fields: date: required: msg: This field is required sfDateValidator: min: 10.10.2007 max: 15.10.2007
You can, of course, specify just one of those parameters to check if a date is greater or lower than the entered date.
The compare parameter now also accepts the strtotime-values instead of just the value of another form field. Combined with the min/max-parameters and the new != operator you can, for example, check if a date is in a specific timeframe but not on a specific day:
fields:
date:
required:
msg: This field is required
sfDateValidator:
min: 'today'
max: '+1 week'
compare: 'next monday'
operator: '!='
This would check if the date is in the next week, but not on monday (Because, well, let’s say you don’t work on mondays)
I also wrote some (69 to be exact) Unit tests for Lime, hoping that this will help for further development.
For the future i really hope that php 5.2 becomes a requirement for the next symfony version, this way we could use the new DateTime-class to overcome the unix-timestamp limitations and check all dates not just these from 1970 to 2038.
I hope this improvements will be useful to you, comments are always appriciated. Thanks.
April 13, 2007 at 7:23 am
Congratz. This is really useful validator.
April 13, 2007 at 10:39 am
Thanks a lot for your efforts. I think that if you create a patch with your validator and submit it to the symfony trac, your validator might even replace the one in the trunk…
May 25, 2007 at 9:47 am
what about the patch? did you commit your changes to the symfony svn?
May 25, 2007 at 10:26 am
There’s a ticket in trac, http://trac.symfony-project.com/trac/ticket/1665
Now it’s up to fabien ;)
June 19, 2007 at 2:35 pm
thank you for the great job. very useful for me.
August 5, 2007 at 5:51 pm
Hey
I was surfing the web and i saw this site, pretty cool.
Currently im running and adult site:Reachton
k, just want to say hi :)
Can i link you from my site? im looking for quality content like yours. If no let me know if i can add u in exchange for a montly fee or something.
November 17, 2007 at 6:24 am
This is great, very useful. I note that the lowest possible year you can go to is -105, after that strtotime() seems to return false.
Any chance we can see a nice little javascript equivalent checker to go with this, so we can add client side checking as well? :-)
March 4, 2008 at 1:18 pm
Hey… Thanks for your validator.. will be really helpful in my project. I can’t wait for symfony1.1 and their newer validators :-)
BTW, did you apply the patch in the download link mentioned here?
May 6, 2008 at 3:10 pm
Does anyone know where to get the finall version of the validator – because the link given to sluug.it do not works.
Thanks.
May 6, 2008 at 6:19 pm
http://trac.symfony-project.com/attachment/ticket/1665/sfMyDateValidator.class.php
May 7, 2008 at 7:41 pm
[...] might want to head over to the old post on Symfoniac for a quick usage [...]
May 21, 2008 at 2:22 pm
2 questions:
1: Has this code by now been integrated into symfony?
2: Is there some way I can compare 2 dates of a form? I have a start and end date and I don’t want start to be beyond end, right?
May 21, 2008 at 3:29 pm
1. No, Symfony 1.1 will feature a completely new form system and also new validators.
2. sure, like this:
fields:
date1:
sfDateValidator:
compare: date2
operator: ‘<=’
date2:
sfDateValidator:
May 22, 2008 at 12:43 am
I take it some spaces were lost there and it should be something like:
fields:
_date1:
_sfDateValidator:
__compare: date2
__operator: ‘<=’
_date2:
__sfDateValidator:
December 3, 2008 at 12:39 pm
good luck and i hope to see a news type of validator
April 15, 2009 at 3:32 pm
After reading the article, I feel that I really need more info. Can you suggest some resources ?