【问题标题】:How to setup optional date field?如何设置可选日期字段?
【发布时间】:2014-11-24 21:04:22
【问题描述】:
我需要使用日期选择器在表单可选日期字段中显示。
但是当我创建新记录时,它会自动填充当前日期。
当我尝试更改它(将其设为空白)时,出现错误:“无效日期”。
我需要机会只选择设置此字段。
它不是强制性的必填字段。你能告诉我我做错了什么吗?
我的实体代码:
class Entity {
...
/**
* @ORM\Column(type="date", nullable=true)
*/
private $birthday = null;
...
}
我的 generator.yml:
birthday:
formType: s2a_date_picker
【问题讨论】:
标签:
forms
date
symfony
doctrine-orm
admin-generator
【解决方案1】:
您的实体/项目中是否有任何验证器文件或规则?
您是否也尝试将表单选项(通过 addFormOption 键)“必需”设置为 false?
=== 编辑
我刚刚创建了以下示例应用:
generator: admingenerator.generator.doctrine
params:
model: Application\CoreBundle\Entity\Account
namespace_prefix: Application
concurrency_lock: ~
bundle_name: AdminBundle
pk_requirement: ~
fields:
id:
filterable: 1
name:
filterable: 1
expirationDate:
formType: s2a_date_picker
object_actions:
delete: ~
batch_actions:
delete: ~
builders:
list:
params:
title: List for AdminBundle
display: ~
filters: ~
actions:
new: ~
object_actions:
edit: ~
delete: ~
excel:
params: ~
filename: ~
filetype: ~
new:
params:
title: New object for AdminBundle
display:
- name
- expirationDate
actions:
save: ~
list: ~
edit:
params:
title: "You're editing the object \"%object%\"|{ %object%: Account.name }|"
display:
- name
- expirationDate
actions:
save: ~
list: ~
show:
params:
title: "You're viewing the object \"%object%\"|{ %object%: Account.name }|"
display: ~
actions:
list: ~
new: ~
actions:
params:
object_actions:
delete: ~
batch_actions:
delete: ~
使用此实体映射:
Application\CoreBundle\Entity\Account:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
expirationDate:
type: datetime
nullable: true
lifecycleCallbacks: { }
当然,以防万一,实体:
<?php
namespace Application\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Account
*/
class Account
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var \DateTime
*/
private $expirationDate = null;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Account
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param \DateTime $expirationDate
* @return $this
*/
public function setExpirationDate(\DateTime $expirationDate = null)
{
$this->expirationDate = $expirationDate;
return $this;
}
/**
* @return \DateTime
*/
public function getExpirationDate()
{
return $this->expirationDate;
}
}
我的实体没有任何特定的自定义,也没有我的管理员配置或其他任何东西......而且效果很好。
它对你有帮助吗?
你是否在你的应用程序中做一些特定的事情?