20

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

Anil Gupta
  • 2,329
  • 4
  • 24
  • 30

4 Answers4

46

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;
}
RobMasters
  • 4,108
  • 2
  • 27
  • 34
  • Exactly what I was looking for. Thanks! – Matheno Jun 10 '15 at 10:22
  • @RobMasters What if the attribute we need to display is in fact a method? – smarber Mar 22 '16 at 11:16
  • When trying to set datetime this way $instance->setCloseTimeUTC((new \DateTime())->format('Y-m-d H:i:s')); on saving gives error "Expected one of the following types: null, DateTime" – Darius.V May 05 '18 at 14:29
7

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

class TheEntity
{
    private $name = 'default name';
}
  • 2
    Why is this answer voted down? It works and involves the least overriding of vendor code. Best answer imho. – FallenSquirrel Feb 23 '15 at 11:48
  • @FallenSquirrel it's work but i think it's not the desired solution as OP probably want a solution to be done in Sonata and don't want to touch the class property – GusDeCooL Aug 03 '17 at 07:25
6

In addition to @RobMasters solution:

If you want to set a relation you can get a reference from the entitymanager (instead of the complete object):

public function getNewInstance()
{
    $instance = parent::getNewInstance();

    if ($this->hasRequest()) {
        $branch = $this->getRequest()->get('branch', null);

        if ($branch !== null) {
            $entityManager = $this->getModelManager()->getEntityManager('MyBundle\Entity\Branch');
            $branchReference = $entityManager->getReference('MyBundle\Entity\Branch', $branch);

            $instance->setBranch($branchReference);
        }
    }
    return $instance;
}

I added the example to my blog: http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/

webDEVILopers
  • 1,886
  • 1
  • 21
  • 35
0

For booleans, another option is to set a data value within the first array passed to your add method, inside of configureFormFields

So after some memtoring, my code (for a checkbox that I wanted to have checked by default) ended up looking something like this:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('visible', null, ['label'=>'Visibility', 'data' => true ])
    ;
}

... which saved a few lines at the top of my file, since I could then get rid of the getNewInstance() definition.