“Fixing” the sfDateValidator

March 21, 2007

I’m back from vacation and just took a look at the sfDateValidator. Basically, this validator works very well but has some restrictions.

The first one is that it only accepts the date in the current i18n format, so for default this validator would only accept a date in the “year-month-day”-format.
Because every user will enter the date in their commonly used format, forcing the user to enter the date in a specific format wouldn’t be very user-friendly. Also i’m not the only one who has noticed this limitation.

The second restriction is the usage of the unix-timestamp for the comparison, which limits the dates to 1970 – 2038, but there doesn’t seem to be a easy alternative to compare dates in php.
The PEAR Date-class looks like it’s capable of handling all dates, but i haven’t had the time to look at it.

However, i reworked the sfDateValidator class a little to have it accept all dates.

Get it here!

Just copy it in your projects lib/ directory and start using it in your validation-files. Here’s a example:

fields:
  date:
    required:
      msg: This field is required
    sfMyDateValidator:
      compare: date2
      operator: ">"
  date2:
    required:
      msg: This field is required
    sfMyDateValidator:

This would check if date1 is greater than date2.

The only thing that concerns me now is that it uses the strtotime-function to convert the date into a unix-timestamp and also accepts relative values like “now”, “tomorrow” or “+1 week”.

Advertisements

10 Responses to ““Fixing” the sfDateValidator”

  1. nautilebleu Says:

    Good job ! It’s clear that sfDateValidator is not the best validator at this time !

  2. Notorious Says:

    Nice adjusment but I can’t get it to work, using this code:
    methods:
    post: [start_at, end_at]
    fillin:
    enabled: true
    names:
    start_at:
    required: Yes
    required_msg: Specify start date
    sfMyDateValidator:
    compare: end_at
    operator: “

  3. Notorious Says:

    Nice adjusment but I can’t get it to work, using this code:
    methods:
    post: [start_at, end_at]
    fillin:
    enabled: true
    names:
    start_at:
    required: Yes
    required_msg: Specify start date
    sfMyDateValidator:
    compare: end_at
    operator:

  4. symfoniac Says:

    Notorious, are you using symfony 1.0?
    To me it seems like you are using the old format for validation files. Did you try it with the example i wrote in the post?

  5. vinilios Says:

    zip link doesn’t seems to work

  6. symfoniac Says:

    vinilios, yes the server is down atm. Should be up in a few hours

  7. Hobbles Says:

    Hi, Is it possible to compare a given date to the current date. I need to test if a date is in the future, but can’t see how…

    Great “fix” though :)

  8. symfoniac Says:

    The only way i would see is saving the current date in a hidden field and then comparing it. But thats not very clean.

    But it’s a nice addition to the sfDateHelper, i think i will implement it when i have the time for it.

    kind regards,
    arthur

  9. Hobbles Says:

    Since it is clear that you did not have this implemented yet I have done a quick fix to fit my needs: (At line 53):
    if($compareDate == “now”)
    $value2 = time();
    else
    $value2 = $this->getValidDate($compareValue);


  10. […] 12th, 2007 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 […]


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: