I can't set the default value of a form field that has a DataTransformer attached to it.
Let's take the example from the official documentation on DataTransformers:
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', TextareaType::class)
->add('issue', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Task::class,
));
}
In the example, a ModelTransformer is added to the 'issue' field, transforming the Issue object into a string (the issue number) when rendering the form, and transforming it back to an Issue upon form submission.
Now I'm setting a default value for the issue field through the builder options:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$issue = $options['issue']; //$issue contains an Issue Object from the controller
$builder
->add('description', TextareaType::class)
->add('issue', TextType::class, array(
'data' => $issue
))
;
}
When rendering the form I get the following error:
The form's view data is expected to be an instance of class AppBundle/Entity/Issue, 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 AppBundle/Entity/Issue.
- Dumping $issue returns an Issue object
- Setting the data_class option to null as suggested returns the same error
- Passing $issue->getId() instead of $issue gets me the following error: "Call to a member function getId() on integer"
I really don't understand this error, I hope you'll be able to help !