Czechnology
Czechnology

Reputation: 14992

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

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)

Upvotes: 10

Views: 21469

Answers (5)

Louwki
Louwki

Reputation: 715

Or In annotations Use:

options={"default":"foo"}

and not:

options={"default"="foo"}

For instance:

/**
 * @ORM\Column(name="foo", type="smallint", options={"default":0})
 */
private $foo;

Upvotes: 1

Freenando
Freenando

Reputation: 405

In Object-oriented programming you should use constructor of the entity to set a default value for an attribute:

public function __construct() {
    $this->sale = false;
}

Upvotes: 6

Yassir Ennazk
Yassir Ennazk

Reputation: 7169

You can also use the 'data' parameter like in :

->add('date', 'date', array(
                    'widget' => 'single_text',
                    'format' => 'dd/MM/yyyy',
                    'attr' => array('class' => 'datepicker'),
                    'data' => new \DateTime()
                ))

Here I have set a class to make a jQuery UI datepicker of the field using JavaScript. I also set the widget to a single_text so I won't get three select fields. And then I set the default data to the current DateTime()

Upvotes: 1

Julien Ducro
Julien Ducro

Reputation: 854

Your boolean value need to have nullable set as true:

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

Upvotes: 15

kgilden
kgilden

Reputation: 10346

I haven't used the CRUD auto-generation tool, but I know that by default, each and every field is required. YOu must explicitly pass 'required' => false as an option for your fields.

This can be done in the form classes

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class FooType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('field', 'text', array('required' => false));
    }

    public function getName()
    {
        return 'foo';
    }
}

The same can be achived in a Form class generated inside your controller

namespace Acme\DemoBundle\Controller;

use Acme\DemoBundle\Entity\Foo;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        // ...    

        $form = $this->createFormBuilder($foo)
            ->add('field', 'text', array('required' => false)
            ->getForm();

        // ...

        return $this->render('AcmeDemoBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

Upvotes: 3

Related Questions