Symfony2表单生成器中实体支持的日期字段的默认值

时间:2013-01-14 09:15:22

标签: forms symfony doctrine symfony-forms

好的,我有下面描述的FormType。我将这个Form类用于创建和编辑表单。我决定使用options数组中的from_date属性设置默认日期(to_datedata下方)。这样做可以很好地设置默认日期,事实上,这个工作太棒了。它还会覆盖编辑表单中的现有日期,这实际上并不好。

如何设置真正的“默认”值,而不是“始终”值?

<?php

namespace TechPeople\InvoiceBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\SecurityContext;


class InvoiceType extends AbstractType
{
    private $user;

    public function __construct(SecurityContext $security_context)
    {
        $this->user = $security_context->getToken()->getUser();
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $lastMonth = new \DateTime();$lastMonth->modify('-1 month');
        $builder
            ->add('month', 'choice', array(
                'data' => $lastMonth->format('F'),
                'choices' => array(
                    "January" => "January",
                    "February" => "February",
                    "March" => "March",
                    "April" => "April",
                    "May" => "May",
                    "June" => "June",
                    "July" => "July",
                    "August" => "August",
                    "September" => "September",
                    "October" => "October",
                    "Novemeber" => "Novemeber",
                    "December" => "December",
                )
            ))
            ->add('year', null, array(
                'data' => $lastMonth->format('Y')
            ))
            ->add('from_date', 'date', array(
                 'label' => 'From',
                 'data' => new \DateTime(),
            ))
            ->add('to_date', 'date', array(
                 'label' => 'To',
                 //'data' => new \DateTime(),
            ))
            ->add('hours')
            ->add('expenses')
            ->add('expense_amount', 'money',
                array(
                    'required' => false,
                ))
            ->add('attachment', 'file',
                array(
                    'path'=>$options['data']->getAttachmentPath(),
                    'required' => false,
                )
            )
        ;
        if($this->user->hasRole('ROLE_ADMIN')){
            $builder->add('vendor');
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TechPeople\InvoiceBundle\Entity\Invoice'
        ));
    }

    public function getName()
    {
        return 'techpeople_invoicebundle_invoicetype';
    }
}

1 个答案:

答案 0 :(得分:4)

在构造函数中将其设置在实体或您将用作表单数据的任何对象上,或者作为默认值设置!

class Invoice {

    private $month;
    private $year;
    private $from_date;
    private $to_date;
    //...

    public function __construct()
    {
        $lastMonth = new \DateTime('now - 1 month');
        $this->month = $lastMonth->format('F');
        $this->year = $lastMonth->format('Y');
        $this->from_date = new \DateTime;
        $this->to_date = new \DateTime;
        //...
    }
}

它将为创建表单设置这两个字段,对于持久化实体,这些值将在加载时被存储的数据覆盖。