I'm trying to set a default value on EntityType select like this:
$builder
->add('town', EntityType::class, array(
'label' => "TOWN",
'class' => 'AppBundle:Town',
'choice_label' => 'name',
'choice_value' => 'id',
'attr' => array('class' => 'form-control'),
'placeholder' => 'CHOOSE_AN_OPTION',
'required' => false,
'data' => $this->em->getReference('AppBundle:Town', $options['attr']['town'])
));
I was trying to do it using data option. It works fot a ChoiceType, but not for EntityType. I tried to pass to the data option the town entity ID, Name and the entire object.
I create the form object from the controller:
$finder = $this->createForm(UserFinderType::class, null,
array('attr' => array('role' => $this->getUser()->getRole(),
'town' => empty($params['town']) ? null : $townService->findOneById($params['town'])->getId() )));
When I pass Town ID or Name nothing happens, placeholder is shown. If I remove placeholder, blank option is selected.
When I pass entire Town object, this error is returned: Catchable Fatal Error: Object of class AppBundle\Entity\Town could not be converted to string
And, if I implement a __toString method, I get the same result that I get on ID or Name try.