Reputation: 367
I am using Symfony3, getting following error when creating form for EntityType::class
My entity namespace Project\CoreBundle\Entity\CountryEntity
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, ['label' => 'Name *', 'attr' => ['data-help' => 'enter language name here.']])
->add('iso', TextType::class, ['label' => 'ISO *', 'attr' => ['data-help' => 'enter language code here.']])
->add('defaultLanguage', EntityType::class,
array(
'class' => 'Project\CoreBundle\Entity\CountryEntity'
)
)
->getForm();
;
}
Throws Catchable Fatal Error: Object of class Project\CoreBundle\Entity\CountryEntity could not be converted to string
Any help will be useful, thanks
Upvotes: 0
Views: 777
Reputation: 275
as EntityType is an extension of ChoiceType, it needs to know how to render your entity to the form. If you do not pass any information it will try to use the __toString()
method. If it is not defined, you'll get this error.
Instead of implementing __toString()
, you may specify the choice_label
option for your field, which should be a path specification to a property, that should be shown. For example, your CountryEntity
class may have a name
property:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, ['label' => 'Name *', 'attr' => ['data-help' => 'enter language name here.']])
->add('iso', TextType::class, ['label' => 'ISO *', 'attr' => ['data-help' => 'enter language code here.']])
->add('defaultLanguage', EntityType::class,
array(
'class' => 'Project\CoreBundle\Entity\CountryEntity',
'choice_label' => 'name'
)
)
->getForm();
;
}
See also EntityType
's documentation at http://symfony.com/doc/current/reference/forms/types/entity.html.
Best regards
Upvotes: 3