"Unable to reverse value for property path" for Symfony2 form ChoiceType field on Select2
<p>Long story short, in Symfony 2.8 I've got <code>Movie</code> entity with <code>actors</code> field, which is <code>ArrayCollection</code> of entity <code>Actor</code> (<code>ManyToMany</code>) and I wanted the field to be ajax-loaded Select2.
When I don't use Ajax, the form is:</p>

<pre><code>-&gt;add('actors', EntityType::class, array(
        'class' =&gt; Actor::class,
        'label' =&gt; "Actors of the work",
        'multiple' =&gt; true,
        'attr' =&gt; array(
          'class' =&gt; "select2-select",
         ),
       ))
</code></pre>

<p>And <em>it works</em>.</p>

<p>I tried to put there an empty Select field:</p>

<pre><code>-&gt;add('actors', ChoiceType::class, array(
    'mapped' =&gt; false,
    'multiple' =&gt; true,
    'attr'=&gt;array(
        'class' =&gt; "select2-ajax",
        'data-entity'=&gt;"actor"
    )
))
</code></pre>

<p>The Select2 Ajax works, everything in DOM looks the same as in previous example, but on form submit I get errors in the profiler: <code>This value is not valid.</code>:</p>

<pre><code>Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[actors] = [0 =&gt; 20, 1 =&gt; 21]
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "actors": Could not find all matching choices for the given values
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Could not find all matching choices for the given values
</code></pre>

<p>The funny part is the data received is the same as they were when it was an <code>EntityType</code>: <code>[0 =&gt; 20, 1 =&gt; 21]</code></p>

<p>I marked field as not mapped, I even changed field name to other than Movie entity's field name. I tried adding empty <code>choices</code>, I tried to leave it as <code>EntityType</code> but with custom <code>query_builder</code>, returning empty collection. Now I'm out of ideas. </p>

<p>How should I do it?</p>

<p><strong>EDIT</strong> after Raymond's answer:</p>

<p>I added DataTransformer:</p>

<p>

<pre><code>use Doctrine\Common\Persistence\ObjectManager;
use CompanyName\Common\CommonBundle\Entity\Actor;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ActorToNumberTransformer implements DataTransformerInterface
{
    private $manager;

    public function __construct(ObjectManager $objectManager)
    {
        $this-&gt;manager = $objectManager;
    }

    public function transform($actors)
    {
        if(null === $actors)
            return array();

        $actorIds = array();
        foreach($actors as $actor)
            $actorIds[] = $actor-&gt;getId();

        return $actorIds;
    }

    public function reverseTransform($actorIds)
    {
        if($actorIds === null)
            return array();

        foreach($actorIds as $actorId)
        {
            $actor = $this-&gt;manager-&gt;getRepository('CommonBundle:Actor')-&gt;find($actorId);
            if(null === $actor)
                throw new TransformationFailedException(sprintf('An actor with id "%s" does not exist!', $actorId));

            $actors[] = $actor;
        }

        return $actors;
    }
}
</code></pre>

<p>Added it at the end of the <code>MovieType buildForm()</code>:</p>

<pre><code>$builder-&gt;get('actors')
    -&gt;addModelTransformer(new ActorToNumberTransformer($this-&gt;manager));
$builder-&gt;get('actors')
    -&gt;addViewTransformer(new ActorToNumberTransformer($this-&gt;manager));
</code></pre>

<p>And added service:</p>

<pre><code>common.form.type.work:
    class: CompanyName\Common\CommonBundle\Form\Type\MovieType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }
</code></pre>

<p>Nothing changed. On form submit, <code>reverseTransform()</code> gets the proper data, but profiler shows the same error. That's a big mistery for me now...</p>