简体   繁体   English

如何在Zend Framework表单中创建日期时间验证器?

[英]How to create a datetime validator in a Zend Framework form?

By default a Zend Framework date validator uses the date format yyyy-MM-dd : 默认情况下,Zend Framework日期验证程序使用日期格式yyyy-MM-dd

$dateValidator = new Zend_Validate_Date();

But I want to add hour and minute validation. 但我想添加小时和分钟验证。 In other words, I want to require the user to enter the hour and minute. 换句话说,我想要求用户输入小时和分钟。 But the following does not work: 但以下不起作用:

$dateValidator = new Zend_Validate_Date('yyyy-MM-dd hh:ii');

If I enter 2010-02-01 , I get an error saying that the date doesn't fit the format. 如果我输入2010-02-01 ,则会收到错误消息,指出日期不符合格式。 If I enter 2010-02-01 3 , it doesn't complain. 如果我输入2010-02-01 3 ,它不会抱怨。 What's it's doing is assuming that the user means 2010-02-01 03:00 rather than enforcing that the user enters the date in the given format. 它正在做的是假设用户意味着2010-02-01 03:00而不是强制用户以给定格式输入日期。

How can I enforce that the date must be entered in the given format? 如何强制必须以给定格式输入日期?

Please see: http://framework.zend.com/issues/browse/ZF-6369 请参阅: http//framework.zend.com/issues/browse/ZF-6369

Basically what it comes down to is that the code underlying the format validation doesn't work correctly. 基本上它归结为格式验证的底层代码无法正常工作。 Rather than using strict validation, it will try to coerce the provided date into something that will validate and so you get hanky results. 它不会使用严格的验证,而是会尝试将提供的日期强制转换为可验证的内容,从而获得手帕效果。

It does look like the bug has been marked as 'Major' so hopefully we'll see a fix soon. 它确实看起来已被标记为'Major',所以希望我们很快就能看到修复。

To add to Noah's answer, Zend_Validate_Date is really quite awful and inflexible; 为了增加诺亚的答案, Zend_Validate_Date非常糟糕Zend_Validate_Date灵活; that is if you want to have a more forgiving policy for date entry. 也就是说,如果你想要一个更宽容的日期条目政策。

Now, if ZF shipped with a Zend_Filter_Date that would normalize the various trivial (albeit very parseable) formats date selectors / user input supplies, that might be a different story as you could filter the date to a normalized format, then validate it that it is in that format. 现在,如果ZF附带了一个Zend_Filter_Date ,它可以规范各种琐碎(虽然非常易于解析)格式的日期选择器/用户输入供应,这可能是一个不同的故事,因为您可以将日期过滤为标准化格式,然后验证它是以那种格式。 But it doesn't. 但事实并非如此。

No matter though, there are plenty of sane solutions to this problem. 不过,无论如何,这个问题都有很多理智的解决方案。 Probably the easiest of which is this: 可能最简单的是:

$validator = new \Zend_Validate_Callback(function($value) {
    return (bool)strtotime($value);
});

Personally, I don't care whether a date / datetime comes in as yyyy/MM/dd, Sept 23, 2012, or as "-2 weeks" -- all I really care about is whether or not strtotime is smart enough to parse it. 就个人而言,我不关心日期/日期是否以yyyy / MM / dd,2012年9月23日或“-2周”的形式出现 - 我真正关心的是strtotime是否足够智能解析它。

+1 to Stephen's answer. 斯蒂芬回答+1。 I went for a similar solution, since I already knew the format I had to validate: 我找了一个类似的解决方案,因为我已经知道我必须验证的格式:

$validator = new \Zend_Validate_Callback(function($value) {

    return (bool) date_create_from_format('Y-m-d H:i', $value);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM