Search code examples
symfonysymfony-sonata

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


Solution

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