How to set a default value in Symfony2 so that automatic CRUD generated forms don't require those fields?

Czechnology picture Czechnology · Sep 14, 2011 · Viewed 21.2k times · Source

As I've already found out, Doctrine2 "does not support to set the default values in columns through the “DEFAULT” keyword in SQL. ... you can just use your class properties as default values".

class Product
{

// ...

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name = "";

    /**
     * @var string $sale
     *
     * @ORM\Column(name="sale", type="boolean")
     */
    private $sale = false;

But even when I do this, the generated CRUD forms still require me to fill out all forms. In case of boolean attributes this even means I can only set it to true (i.e. 1).

Am I doing something wrong?

(I know I can turn the validation off but I'd like a solution to the problem instead of just bypassing it)

Answer

Julien Ducro picture Julien Ducro · Oct 3, 2011

Your boolean value need to have nullable set as true:

/**
 * @var string $sale
 *
 * @ORM\Column(name="sale", type="boolean", nullable=true)
 */
private $sale = false;