gpt4 book ai didi

forms - symfony2 : null date rendering

转载 作者:行者123 更新时间:2023-12-02 00:20:59 25 4
gpt4 key购买 nike

大家

我在 Symfony2 中遇到空日期和表格的问题。

当我创建一个日期为空的实体时,它工作正常,一个 NULL 值被插入到数据库中。但是当我想编辑它时,它呈现为今天,我找不到呈现 empy_values 的方法

正如预期的那样,“preferred_choices”不起作用,因为“date”不是“choice”。

似乎在某处调用了一个新的\DateTime()。

Index和show Action 没有问题:

[index/show.html.twig]

            {% if entity.dueDate %}

{{ entity.dueDate|date('Y-m-d') }}

{% endif %}

如果我在 Controller 中询问,行为是预期的

[ Controller ]

if (!$entity->getDueDate()) {

// enters here when there is NULL in the database

}

下面是实体和表单定义:

[实体]

/**

* @var date $dueDate

*

* @ORM\Column(name="dueDate", type="date", nullable="true")

*/

private $dueDate;

[表格]

  $builder->add('dueDate', 'date', array('label'=>'Due date', 'empty_value' => array('year' => '----', 'month' => '----', 'day' => '----'),'required'=>false))

请指点一下,先谢谢了。

2011-06-26 有一个相关问题在 google groups 中没有答案

https://groups.google.com/forum/#!msg/symfony2/nLUmjKzMRVk/9NlOB1Xl5RwJ

http://groups.google.com/group/symfony2/browse_thread/thread/9cb5268caccc4559/1ce5e555074ed9f4?lnk=gst&q=empty+date+#1ce5e555074ed9f4

最佳答案

对于现代版本的 Symfony,您似乎需要:

$builder->add('dueDate', DateType::class, array(
'placeholder' => ['year' => '--', 'month' => '--', 'day' => '--']
)

empty_value 已替换为 placeholder,您需要传递一个包含每个“空”值的数组。

关于forms - symfony2 : null date rendering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com