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

Download the validator here!

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 “”-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:


      msg: This field is required
      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:

      msg: This field is required
      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.


16 Responses to “My final sfDateValidator”

  1. Damjan Says:

    Congratz. This is really useful validator.

  2. 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…

  3. Markus Says:

    what about the patch? did you commit your changes to the symfony svn?

  4. symfoniac Says:

    There’s a ticket in trac,

    Now it’s up to fabien ;)

  5. yoyo Says:

    thank you for the great job. very useful for me.

  6. Music-Band Says:


    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.

  7. gunnar Says:

    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? :-)

  8. munim Says:

    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?

  9. scythe Says:

    Does anyone know where to get the finall version of the validator – because the link given to do not works.

  10. […] might want to head over to the old post on Symfoniac for a quick usage […]

  11. 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?

  12. Arthur Says:

    1. No, Symfony 1.1 will feature a completely new form system and also new validators.

    2. sure, like this:

    compare: date2
    operator: ‘<=’

  13. I take it some spaces were lost there and it should be something like:

    __compare: date2
    __operator: ‘<=’

  14. aabbassi Says:

    good luck and i hope to see a news type of validator

  15. After reading the article, I feel that I really need more info. Can you suggest some resources ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: