如何设置可选日期字段?

时间:2014-11-24 21:04:23

标签: forms date symfony doctrine-orm admin-generator

我需要在表单中显示带日期选择器的可选日期字段。 但是当我创建新记录时,它会自动填充当前日期持续存在。 当我尝试更改它(使其空白)我有错误:“无效的日期”。 我需要机会只选择设置此字段。 它不是必填字段。你能告诉我我做错了什么吗?

我的实体代码:

class Entity {
...
/**
 * @ORM\Column(type="date", nullable=true)
 */
private $birthday = null;
...
}

我的generator.yml:

birthday:
    formType:    s2a_date_picker

1 个答案:

答案 0 :(得分:1)

您的实体/项目中是否有任何验证器文件或规则?

您是否也尝试将表单选项(通过addFormOption键)'requiered'设置为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;
    }
}

并且我的实体上没有特定的自定义我的管理员配置或其他任何东西......并且它运作良好。

它对你有帮助吗? 你在申请中做了哪些具体事情吗?