Symfony2 Form Builder中实体支持的日期字段的默认值 [英] Default value for entity backed Date field in Symfony2 Form Builder

查看:596
本文介绍了Symfony2 Form Builder中实体支持的日期字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有下面描述的FormType。我使用这个Form类创建和编辑表单。我决定设置一个默认日期( from_date to_date ),使用 data 属性在选项数组中。这在确定默认日期方面做得很好,实际上,这是一份太好的工作。它也覆盖了编辑表格中的现有日期,这实在是不好。



如何设置真正的默认值,而不是'always'value?

 <?php 

命名空间TechPeople\InvoiceBundle\Form;

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


class InvoiceType extends AbstractType
{
private $ user;

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

$ b $ public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ lastMonth = new \DateTime(); $ lastMonth->修改('-1个月');
$ builder
- > add('month','choice',array(
'data'=> $ lastMonth-> format('F'),
'选择'=>数组(
January=>January,
February=>February,
March=> ,
April=>April,
May=>May,
June=>June =七月,
八月=>八月,
九月=>九月,
十月=> b $ bNovemeber=>Novemeber,
December=>December,

))
- > add('year' ,null,数组(
'data'=> $ lastMonth->格式('Y')
))
- > add('from_date','date',array(
'label'=> 'From',
'data'=>新的\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';


$ / code>


解决方案

Set它在实体上,或任何对象,你将用作表单数据,在构造函数中或作为默认值!

  class Invoice { 

私人$月;
私人$年;
private $ from_date;
private $ to_date;
// ...

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

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


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';
    }
}

解决方案

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.

这篇关于Symfony2 Form Builder中实体支持的日期字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆