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

1
  • Whats the fieldType for name? Commented May 23, 2012 at 5:58

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@RobMasters What if the attribute we need to display is in fact a method?
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"
7

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

class TheEntity
{
    private $name = 'default name';
}

2 Comments

Why is this answer voted down? It works and involves the least overriding of vendor code. Best answer imho.
@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
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/

4 Comments

Where would the identifier resp. "11" come from?
Yeah, my question exactly.
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.
Ty your blog was very helpfull for me <3
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.

1 Comment

While editing existing item value from 'data' will override value from entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.