我在项目中使用Symfony Form和Validator独立组件。我看不出date_format选项将如何用于根据输入值进行验证。
我将字段渲染为:
->add('from_date', 'datetime', array(
'constraints' => array(
new Assert'DateTime(),
new Assert'GreaterThan(array(
'value' => time(),
'message' => 'From: date/time must be in the future'
))
),
'required' => true,
'label' => 'From date:',
'date_widget' => 'single_text',
'time_widget' => 'choice',
'input' => 'timestamp',
'date_format' => 'dd/MM/yyyy',
'attr' => array(
'class' => 'textbox',
'size' => 15
),
'mapped' => false
))
它正确地显示了一个输入框,该输入框根据日期格式(例如2013年12月12日)正确地进行验证,并显示了小时和分钟的两个下拉框。
然而,在提交时,日期时间验证器总是失败——似乎是因为
const PATTERN = '/^('d{4})-('d{2})-('d{2}) ... etc
永远不会更改以处理正确创建的时间戳。因此,这个验证检查总是失败:
if (!preg_match(static::PATTERN, $value, $matches) ||
!checkdate($matches[2], $matches[3], $matches[1])) {
因为$value现在是一个时间戳,而不是输入的格式。
我不确定这是否只是一个问题,或者在独立使用表单时缺少功能,但我看不出有什么方法可以正确验证该配置。
您是否尝试将值设置为''DateTime对象?
"value" => new 'DateTime("NOW"),