表单中的默认日期(symfony2)

时间:2012-05-02 08:27:14

标签: forms symfony

我使用Symfony2创建一个这样的表单:

$builder
                ->add('days', 'date', array(
                    'widget' => 'choice',
                                    'format' => 'dd-MM-yyyy',
                                    'pattern' => '{{ day }}-{{ month }}-{{ year }}',
                                    'years' => range(Date('Y'), 2010),
                                    'label' => 'Inactive participants since',
                                    'input' => 'string',
                    ));

但是我想显示一个默认日期,例如今天,所以,当我打印de form时,我看到了

  

02 - 05 - 2012

有什么想法吗?

3 个答案:

答案 0 :(得分:4)

这适用于约会吗?

 $builder
                    ->add('days', 'date', array(
                        'widget' => 'choice',
                                        'format' => 'dd-MM-yyyy',
                                        'pattern' => '{{ day }}-{{ month }}-{{ year }}',
                                        'years' => range(Date('Y'), 2010),
                                        'label' => 'Inactive participants since',
                                        'input' => 'string',
                                        'data'  => '01-01-2001'
                        ));

答案 1 :(得分:1)

您必须在表单前设置默认日期。

创建实体($entity = new Entity();)时,只需添加如下默认值:

$entity->setDays(my_value);

如果您想添加今天的日期,可以像这样使用DateTime函数:

$entity->setDays(new \DateTime());

希望我足够清楚。

答案 2 :(得分:0)

对于2.1,表单将默认为今天的日期:

 $builder
                    ->add('days', 'date', array(
                        'widget' => 'choice',
                                        'format' => 'dd-MM-yyyy',
                                        'pattern' => '{{ day }}-{{ month }}-{{ year }}',
                                        'years' => range(Date('Y'), 2010),
                                        'label' => 'Inactive participants since',
                                        'input' => 'string',
                                        'data'  => date_create()
                        ));
相关问题