只有在没有传递对象时才默认日期字段类型
Date field type default only when no object is passed
我正在尝试我的第一个 Symfony 应用程序,但是当我处理表单时,发生了一些违反直觉的事情。
正如您从我的代码中看到的那样,我有一个默认为当天的日期字段。但是当我将对象传递给表单时,此默认值会覆盖对象的当前日期。
我知道这是应该发生的事情('The default values for form fields are taken directly from the underlying data structure (e.g. an entity or an array). The data option overrides this default value.',来自 http://symfony.com/doc/current/reference/forms/types/date.html#data)。
有没有什么方法可以抑制这种行为,并且只在没有传递任何对象时显示默认值?
$builder
// other code
->add('date', 'date', array(
'data' => new \DateTime()
))
// other code
我可能会直接在我的新实体中设置它,而不是固定在一个表单中
class YourClass
{
private $date;
//...
public function __construct()
{
$this->date = new \DateTime;
}
}
我正在尝试我的第一个 Symfony 应用程序,但是当我处理表单时,发生了一些违反直觉的事情。
正如您从我的代码中看到的那样,我有一个默认为当天的日期字段。但是当我将对象传递给表单时,此默认值会覆盖对象的当前日期。
我知道这是应该发生的事情('The default values for form fields are taken directly from the underlying data structure (e.g. an entity or an array). The data option overrides this default value.',来自 http://symfony.com/doc/current/reference/forms/types/date.html#data)。
有没有什么方法可以抑制这种行为,并且只在没有传递任何对象时显示默认值?
$builder
// other code
->add('date', 'date', array(
'data' => new \DateTime()
))
// other code
我可能会直接在我的新实体中设置它,而不是固定在一个表单中
class YourClass
{
private $date;
//...
public function __construct()
{
$this->date = new \DateTime;
}
}