0

I am trying to use the select2 for my symfony2 entity fields.

I created an extension of the entity field, a twig template and a Model Transformer for it. Rendering works just fine, however I am having problems with submitting the fields.

With the origin format, the values are sent like that:

Content-Disposition: form-data; name="com_bundle_book[series]"  1 

And for an entity with multiple s

Content-Disposition: form-data; name="com_bundle_book[author][]" 1
Content-Disposition: form-data; name="com_bundle_book[author][]" 3

select2's behaviour is to replace the existing field with a hidden field. The sent data looks like that:

Content-Disposition: form-data; name="com_bundle_book[series]" TraLiRo 
Content-Disposition: form-data; name="com_bundle_book[author][]" Ferdinand,Hans

I hoped to sort this out with the modelTransformer, but instead I get an error (invalid value) for both fields while the modelTransformer is never called.

I tried it with an extension of entity and by extending entity itself. My services:

com_bundle.tag_type_extension:
    class: ...\Form\Extension\TagTypeExtension
    tags:
        - { name: form.type_extension, alias: entity }
    arguments: [@doctrine.orm.entity_manager]
com_bundle.form.type.tagType_entity:
    class: ...\Form\Extension\TagTypeExtension2
    tags:
        - { name: form.type, alias: tagType_entity }
    arguments: [@doctrine.orm.entity_manager]

The classes:

How do I fix this?

Update

Form is built like this:

   $builder
        ->add('author',
            'entity',
            array(
                'select2' => true,
                'multiple' => true,
                'class' => 'Bundle:Author',
                'property' => 'name'
            )
        )
2
  • You should post the code of your Transformer and the lines where you try to activate it. Commented Oct 22, 2014 at 12:11
  • @AlterPHP Absolutely, I hope that helps. Been stuck on that since a week... Commented Oct 22, 2014 at 12:31

1 Answer 1

1

Your Transformer should not return object but ID of object, as default entity ModelTransformer still applies and will transform those IDs into objects.

The Doctrine entity Model Transformer applies before your one that's why your never reach it. Add it as a ViewTransformer like this :

$builder->addViewTransformer(
    new ObjectListToStringTransformer(
        $this->em,
        [
            'class' => $options['class'],
            'property' => $options['property'],
            'delimiter' => ','
        ]
    ), true
);

I added a TRUE as second argument of addViewTransformer => it forces your Transformer to prepend compared to other ones defined by parent field's type.

EDIT: This way you'll have to adapt your Transformer transform() method as it will now receive array of IDs and not array of objects...

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that brings light into dark. But my transform and reverseTransform methods are still not even called at all. Why?
So try not prepending the ViewTransformer by removing the argument "true".
Please show the code that build the Form itself too.
Actually the transform method works now. I am now able to experiment again... Will post back.
This worked, thanks. Had to rework the Transformer, but using a ViewTransformer instead of a ModelTransformer was the trigger! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.