我正在项目中使用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日),以及两小时和分钟的下拉列表。
但是,在提交时,datetime验证器总是失败 - 看似是因为
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) ... etc
永远不会更改来处理正确创建的时间戳。因此,此验证检查始终失败:
if (!preg_match(static::PATTERN, $value, $matches) ||
!checkdate($matches[2], $matches[3], $matches[1])) {
因为$ value现在是时间戳,而不是输入的格式。
在使用独立窗体时,我不确定这只是一个问题还是缺少功能,但我无法看到该配置能够正确验证的方式。
答案 0 :(得分:0)
您是否尝试将值设置为\ DateTime对象?
"value" => new \DateTime("NOW"),