2

I'm validating a user-entered date string in YYYY-MM-DD format using Zend_Validate::is($value,'Date').

This call creates this hierarchy:

Zend_Validate::is()
Zend_Validate_Date->isValid()
Zend_Date::isDate()
Zend_Locale_Format::getDate()
Zend_Locale_Format::_parseDate() 

Finally, it fails with this exception:

Zend_Locale_Exception: Unable to parse date '2009-09-08' using 'MMM d, y' (M <> y) in /usr/share/php/Zend/Locale/Format.php on line 1001

I'm using en_US as my application locale. How can I configure Zend_Validate to accept this date format? Is it possible to change the locale format of the date, for example?

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
farzad
  • 8,775
  • 6
  • 32
  • 41

2 Answers2

9

Try that:

$validator = new Zend_Validate_Date('YYYY-MM-DD');
if($validator->isValid($value))
    // yay
janoliver
  • 7,744
  • 14
  • 60
  • 103
  • thanks a lot. I've never used individual Zend_Validate_X classes before. this is more customizable. – farzad Sep 08 '09 at 08:33
  • 9
    You should use `'yyyy-MM-dd'` though... YYYY is "ISO Year" (which is subtly different near the beggining of the year) and "DD" day of year not day of month. http://framework.zend.com/manual/en/zend.date.constants.html#zend.date.constants.selfdefinedformats.table – gnarf Sep 08 '09 at 09:00
  • If you have other validators or a custom validator on the same element, it would be advisable to add this validator as the second (or last) – tread Mar 07 '14 at 14:14
5

zend_validate_date has a bug , when you specify following yyyy-MM-dd format and if a date string 2011-10-11 12312 is passed on for validation, it returns true instead of false !!

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
psakrii
  • 51
  • 1
  • 2