Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Validate_Date just doesn't work properly

It appears that Zend_Validate_Date just simply doesn't work properly. For example:

$validator = new Zend_Validate_Date(array('format' => 'yyyy'));

This is a simple validator that should only accept a four digit year, yet $validator->isValid('1/2/3') returns true! Really, Zend?

Or how about this:

$otherValidator = new Zend_Validate_Date(array('format' => 'mm/dd/yyyy'));

Even with the above code, $otherValidator->isValid('15/13/10/12/1222') returns true too!

I'm using Zend Framework 1.11.9. Is it just me or is this a really terrible validation class? (UPDATE: In other words, is there something wrong with my code, or is this a bug that should be submitted?)

like image 415
fronzee Avatar asked Jan 12 '12 17:01

fronzee


1 Answers

As the comments above say, apparently there's a bug with this class. Here is the workaround I came up with, using Zend_Validate_Regex:

$validator = new Zend_Validate_Regex(
    array('pattern' => '/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/')
);
$validator->setMessage(
    "Date does not match the format 'mm/dd/yyyy'",
    Zend_Validate_Regex::NOT_MATCH
);

Hopefully that will help someone else. Please note that I only want slashes as separator, not dots or dashes.

like image 85
fronzee Avatar answered Nov 14 '22 22:11

fronzee