Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

symfony

crud

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)

like image 604
Czechnology Avatar asked Sep 13 '11 23:09

Czechnology


4 Answers

Your boolean value need to have nullable set as true:

/**
 * @var string $sale
 *
 * @ORM\Column(name="sale", type="boolean", nullable=true)
 */
private $sale = false;
like image 124
Julien Ducro Avatar answered Oct 20 '22 04:10

Julien Ducro


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;
}
like image 40
Freenando Avatar answered Oct 20 '22 03:10

Freenando


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(),
        ));
    }
}
like image 22
kgilden Avatar answered Oct 20 '22 03:10

kgilden


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()

like image 39
Yassir Ennazk Avatar answered Oct 20 '22 05:10

Yassir Ennazk