I have a form type (field_type) which extends text and have a data_class. Passing an entity instance to the form via event listener leads to a LogicException:
The form's view data is expected to be an instance of class Entity, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Entity.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('field', 'field_type', $opts);
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use($options)
{
...
$form = $event->getForm();
$form->get('field')->setData($entity);
});
}
$entity
is an instance of the data_class. The form type has a view data transformer, too.
Field type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer($this->viewTransformer, true);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Entity',
'invalid_message' => 'The given id is invalid!',
'required' => true
));
}
public function getParent()
{
return 'text';
}
Everything works fine except the part of the data_class. If I remove the data_class it works.
Why do I need to remove this part?