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 ???
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:
For booleans, another option is to set a
data
value within the first array passed to youradd
method, inside ofconfigureFormFields
So after some memtoring, my code (for a checkbox that I wanted to have checked by default) ended up looking something like this:
... which saved a few lines at the top of my file, since I could then get rid of the getNewInstance() definition.
you can also assign the default value to the property of the entity directly:
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):
I added the example to my blog: http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/