Zend_Validate_Date just doesn't work properly

自古美人都是妖i 提交于 2019-12-07 04:16:55

问题


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:


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.



来源:https://stackoverflow.com/questions/8839489/zend-validate-date-just-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!