Symfony 具有默认值的表单类型日期

Symfony 具有默认值的表单类型日期,symfony,Symfony,我想创建一个只有年份选择器的日期字段。首先,我以这样的形式创建这个字段 ->add('visitDate', 'date', array('label' => false, 'translation_domain' => 'messages', 'required'

我想创建一个只有年份选择器的日期字段。首先,我以这样的形式创建这个字段

->add('visitDate',
                       'date',
                        array('label' => false,
                              'translation_domain' => 'messages',
                              'required'    => false,
                              'widget'      => 'choice',
                              'input'       => 'array',
                              'years'       => range(date('Y'), 2006),
                              'months'      => range(1,12),
                              'days'        => range(1,31),
                              'empty_value' => array('month'=> 1, 'day' => 1),
                        )
并将月和日字段隐藏在细枝中。如果我发布表单,则月和日字段的post值为“null”,而不是1


有人能告诉我为什么会发生这种情况吗?

你好像有打字错误

改变

                          'months'      => range(1,12),
                          'days'        => range(1,31),
                          'empty_value' => array('month'=> 1, 'day' => 1),

应该是这样,, 否则,如果这不起作用,如果你想让用户只选择你可以做某事的年份,你就有上千种方法去罗马

// let the user post the $year as string "2015"
$time = new DateTime();
$time->modify('first day of January '.$year);

不幸的是,输入错误并不能解决我的问题。我在哪里设置这个$time值?在表单或控制器中?此修复我的问题“数据”=>日期('Y-m-d'),
// let the user post the $year as string "2015"
$time = new DateTime();
$time->modify('first day of January '.$year);