Zend_Validate_Date just doesn't work properly

2019-05-01 17:09发布

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

1条回答
做自己的国王
2楼-- · 2019-05-01 17:58

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.

查看更多
登录 后发表回答