Search code examples
formsdatesymfonydoctrine-ormadmin-generator

How to setup optional date field?


I need to display in form optional date field with date picker. But when I create new record it's automatically filled with current date on persist. When I try to change it(make it blank) I've got error: "Invalid date". I need opportunity to only optionally set this field. It is NOT mandatory required field. Can you please tell me what am I doing wrong?

My Entity code:

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

My generator.yml:

birthday:
    formType:    s2a_date_picker

Solution

  • Do you have any validator file or rules on your entity/in your project?

    Did you also tried to set the form option (via addFormOption key) 'requiered' to false?

    === Edit

    I just created the following sample app:

    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: ~
    

    With this entity mapping:

    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: {  }
    

    And of course, just in case, the entity:

    <?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;
        }
    }
    

    And no specific customization on my entity neither my admin config or anything else... and it works well.

    Does it helped you? Are you doing something specific in your application?