Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting default value in symfony2 sonata admin bundle

how can i set default value in sonata admin bundle the data option is missing in configureFormFields method

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', null, array('required' => true, 'data' => "my default value"))
    ;
}

how can use data attribute to set default value inside field ???

like image 523
Anil Gupta Avatar asked May 11 '12 06:05

Anil Gupta


2 Answers

I presume you've probably already solved this by now, but as a reference to anyone else you can override the getNewInstance() method and set the default value on the object:

public function getNewInstance()
{
    $instance = parent::getNewInstance();
    $instance->setName('my default value');

    return $instance;
}
like image 82
RobMasters Avatar answered Oct 15 '22 03:10

RobMasters


you can also assign the default value to the property of the entity directly:

class TheEntity
{
    private $name = 'default name';
}
like image 40
Jesse van Muijden Avatar answered Oct 15 '22 05:10

Jesse van Muijden