setting default value in symfony2 sonata admin bundle
Asked Answered
C

4

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

Circumcision answered 11/5, 2012 at 6:0 Comment(1)
Whats the fieldType for name?Utopianism
P
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;
}
Physiognomy answered 27/6, 2012 at 12:17 Comment(2)
@Physiognomy What if the attribute we need to display is in fact a method?Negligible
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"Rhoden
B
7

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

class TheEntity
{
    private $name = 'default name';
}
Bookbinder answered 15/2, 2014 at 16:40 Comment(2)
Why is this answer voted down? It works and involves the least overriding of vendor code. Best answer imho.Ailis
@Ailis 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 propertyBriarroot
T
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/

Typeset answered 3/9, 2014 at 14:16 Comment(4)
Where would the identifier resp. "11" come from?Typeset
Yeah, my question exactly.Arletha
Not sure what you mean. In my example was just hardcoded because I copied it from a test case. Simply replace the 11 with $branch which comes from the request stack. I thought this was pretty obvious. I will edit my example in addition to that.Typeset
Ty your blog was very helpfull for me <3Capstan
B
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.

Burkholder answered 27/2, 2018 at 11:19 Comment(1)
While editing existing item value from 'data' will override value from entity.Lyre

© 2022 - 2024 — McMap. All rights reserved.