在symfony2表单的Datetime字段中设置默认值

时间:2022-10-22 17:03:49

I have a form containing several fields. One of them is a Datetime field. How to define a default value for that field?

我有一个包含几个字段的表单。其中一个是Datetime字段。如何定义该字段的默认值?

I've tried setting a value on the related entity, in controller, in constructor and __construct :

我已经尝试在相关实体,控制器,构造函数和__construct中设置一个值:

$myEntity = new MyEntity();
$myEntity->setMyDate(new \DateTime());
$form = $this->createForm(new AddMyEntity(), $myEntity);

Not working.

不工作。

Tried to define the $data variable in the buildForm :

试图在buildForm中定义$ data变量:

$builder->add('myDate', 'date', array(
    'format' => \IntlDateFormatter::SHORT,
    'input' => 'datetime',
    'widget' => 'single_text',
    'data' => new \DateTime("now"));

Not working either. Any ideas, Symfony2 community?

也不工作。任何想法,Symfony2社区?

EDIT : Adding entity on demand of faost.

编辑:根据要求添加实体。

/**
 * @ORM\Column(name="myDate", type="datetime")
 * @Assert\NotBlank()
 */
private $myDate;

4 个解决方案

#1


62  

Set it in the entity constructor:

在实体构造函数中设置它:

class Entity
{
    /**
     * @var \DateTime
     */
    private $date;

    public function __construct()
    {
        $this->date = new \DateTime();
    }
}

#2


27  

Elnur's answer is correct and is perhaps the recommended one. But for completeness, an alternative way to set the default value for a date widget in a form is to specify the data key in the options array argument with an instance of DateTime.

Elnur的答案是正确的,也许是推荐的答案。但是为了完整性,在表单中设置日期窗口小部件的默认值的另一种方法是使用DateTime实例在options数组参数中指定数据键。

$builder->add('myDate', 'date', array(
    'data' => new \DateTime()
));

Note: This will overwrite the previously set datetime on every edit.

注意:这将覆盖每次编辑时先前设置的日期时间。

#3


5  

This solution doesn't require modifying your entity object.

此解决方案不需要修改您的实体对象。

    $builder->add('myDate', DateTimeType::class, [
        'label' => 'My Date',
        'required' => false,
        'date_widget' => 'single_text',
        'time_widget' => 'single_text',
        'date_format' => 'dd/MM/yyyy'
    ]);

    $builder->get('myDate')->addModelTransformer(new CallbackTransformer(
        function ($value) {
            if(!$value) {
                return new \DateTime('now +1 month');
            }
            return $value;
        },
        function ($value) {
            return $value;
        }
    ));

This solution applies the behaviour to just this form, it does not couple this behaviour to the entity itself. You might have several forms that modify an entity with different required behaviours. Some require a default date, others don't.

此解决方案仅将此行为应用于此表单,它不会将此行为与实体本身相关联。您可能有几种形式可以修改具有不同所需行为的实体。有些需要默认日期,有些则不需要。

#4


-4  

You can set the attributes to on update CURRENT_TIMESTAMP and defualt to current time stamp will update the current time stamp automatically without updating through query

您可以将属性设置为更新CURRENT_TIMESTAMP并将defualt设置为当前时间戳将自动更新当前时间戳而不通过查询更新

`feildname` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP

#1


62  

Set it in the entity constructor:

在实体构造函数中设置它:

class Entity
{
    /**
     * @var \DateTime
     */
    private $date;

    public function __construct()
    {
        $this->date = new \DateTime();
    }
}

#2


27  

Elnur's answer is correct and is perhaps the recommended one. But for completeness, an alternative way to set the default value for a date widget in a form is to specify the data key in the options array argument with an instance of DateTime.

Elnur的答案是正确的,也许是推荐的答案。但是为了完整性,在表单中设置日期窗口小部件的默认值的另一种方法是使用DateTime实例在options数组参数中指定数据键。

$builder->add('myDate', 'date', array(
    'data' => new \DateTime()
));

Note: This will overwrite the previously set datetime on every edit.

注意:这将覆盖每次编辑时先前设置的日期时间。

#3


5  

This solution doesn't require modifying your entity object.

此解决方案不需要修改您的实体对象。

    $builder->add('myDate', DateTimeType::class, [
        'label' => 'My Date',
        'required' => false,
        'date_widget' => 'single_text',
        'time_widget' => 'single_text',
        'date_format' => 'dd/MM/yyyy'
    ]);

    $builder->get('myDate')->addModelTransformer(new CallbackTransformer(
        function ($value) {
            if(!$value) {
                return new \DateTime('now +1 month');
            }
            return $value;
        },
        function ($value) {
            return $value;
        }
    ));

This solution applies the behaviour to just this form, it does not couple this behaviour to the entity itself. You might have several forms that modify an entity with different required behaviours. Some require a default date, others don't.

此解决方案仅将此行为应用于此表单,它不会将此行为与实体本身相关联。您可能有几种形式可以修改具有不同所需行为的实体。有些需要默认日期,有些则不需要。

#4


-4  

You can set the attributes to on update CURRENT_TIMESTAMP and defualt to current time stamp will update the current time stamp automatically without updating through query

您可以将属性设置为更新CURRENT_TIMESTAMP并将defualt设置为当前时间戳将自动更新当前时间戳而不通过查询更新

`feildname` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP