Default value for entity backed Date field in Symf

2019-08-06 01:43发布

问题:

Okay, I've got the FormType described below. I use this Form class for both the create and edit forms. I decided to set a default date (from_date and to_date below), using the data attribute in the options array. This does a great job of setting the default date, in fact, too good a job. It also overrides the existing date in the edit form, which is no good at all, really.

How do I set a real 'default' value, as opposed to an 'always' value?

<?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:

Set it on the entity, or any object you will use as form data, in the constructor or as default value !

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;
        //...
    }
}

It will set up these 2 fields for a creation form, and in the case of a persisted entity, these values will be overriden by stored data at loading.