Thursday, 8 August 2013

Symfony2's DataTransformer in a complicated form

Symfony2's DataTransformer in a complicated form

I have two entites related in OneToMany and ManyToOne way (Category and
Product) and I'm ebedding a collection of forms. So my form looks like
this:
$form = $this->formFactory->create(new CategoryType(), $category);
And in CategoryType a have a field, called products of type collection.
In ProductType I have a hidden filed like this:
$builder->add('seller', 'hidden')->addModelTransformer($myTransformer);
The hidden field is populated with the id of the seller with js and then I
want to transform it to obejct of type Seller.
The problem is the entityManager. Because my form is about Category, doing
this:
$form = $this->formFactory->create(new CategoryType(), $category, array(
'em' => $this->em));
doesn't do the job.
I read that I can define the Transformer as service, too, but using it it
the way it's shown in the doc:
$builder
->add('task')
->add('dueDate', null, array('widget' => 'single_text'))
->add('issue', 'issue_selector');
as custom field type also isn't good, because I want my field to be of
type hidden.
Can you please tell if there is a solution to this problem :(

No comments:

Post a Comment