Zend_Validate_Date不能正常工作 [英] Zend_Validate_Date just doesn't work properly
问题描述
似乎Zend_Validate_Date
只是无法正常工作.例如:
It appears that Zend_Validate_Date
just simply doesn't work properly. For example:
$validator = new Zend_Validate_Date(array('format' => 'yyyy'));
这是一个简单的验证器,只应接受四位数的年份,而$validator->isValid('1/2/3')
返回true!真的,Zend吗?
This is a simple validator that should only accept a four digit year, yet $validator->isValid('1/2/3')
returns true! Really, Zend?
或者这样:
$otherValidator = new Zend_Validate_Date(array('format' => 'mm/dd/yyyy'));
即使使用上面的代码,$otherValidator->isValid('15/13/10/12/1222')
也会返回true
!
Even with the above code, $otherValidator->isValid('15/13/10/12/1222')
returns true
too!
我正在使用Zend Framework 1.11.9.是我还是这真是可怕的验证课程? (更新:换句话说,我的代码是否有问题,还是应该提交的错误?)
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?)
推荐答案
正如上面的评论所述,此类显然存在一个错误.这是我使用Zend_Validate_Regex
想到的解决方法:
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.
这篇关于Zend_Validate_Date不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!