2012-05-02 46 views
3

我创建一个表单,像这样用的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', 
        )); 

但我想显示默认的日期,今天为例,所以,当我打印德形式我看到

02 - 05 - 2012

任何想法?

回答

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' 
         )); 
+0

它不起作用 - '预期的参数类型为“\ DateTime”,“string”给出' –

+0

为我工作在2.1 –

+1

@Asuraya如果你明白了,你还没有设置它使用'string'。在这种情况下,使用:'array('data'=> new \ DateTime())' – Prisoner

1

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

当您创建实体($entity = new Entity();),只要将你的默认值是这样的:

$entity->setDays(my_value);

如果你想添加今天的日期,你可以使用日期时间函数是这样的:

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

希望我很清楚。

+0

的一点是如何做到这一点没有一个类(实体)associed这种形式。任何想法? – Bernat

+1

好问题,我不确定,但你可以尝试这个例子:http://symfony.com/doc/current/book/forms.html#using-a-form-without-a-class并添加你的默认('days'=> new \ DateTime());' – Snroki

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() 
         ));