Symfony3 Tetranz/select2 "Unable to reverse value for property path"
<p>I use <code>*tetranz/select2entity-bundle&quot;: &quot;2.*&quot;*</code> in my project to select a town (<em>commune</em> in French). As there are almost 40,000 towns in France, Ajax is mandatory. But the Bundle doesn't work out-of-the-box.</p>
<p>There are several threads on the web that seem to fit my issue but none seems clear enough for me as I'm new in <code>Symfony</code>, and version 3 is not quite documented.</p>
<p><strong>In the profiler, I get this issue :</strong></p>
<blockquote>
<p>Symfony\Component\Validator\ConstraintViolation</p>
<p>Object(Symfony\Component\Form\Form).children[address].children[commune] = 30223</p>
<p>Caused by: Symfony\Component\Form\Exception\TransformationFailedException</p>
<p>Unable to reverse value for property path &quot;commune&quot;: The choice &quot;30223&quot; does not exist or is not unique</p>
<p>Caused by: Symfony\Component\Form\Exception\TransformationFailedException</p>
<p>The choice &quot;30223&quot; does not exist or is not unique</p>
</blockquote>
<p>So this error means that my choice (town of Chambéry, ID 30223) is not part of the available choices on form build. Ok. But the bundle is supposed to handle this and display the selection after form submit. It does not display my choice when I submit the form and get this error.</p>
<p>So, what I did was hack into the <code>Twig field template</code> and <code>dump()</code> the value variable -&gt; empty. Ok.</p>
<p>I also dumped the POST vars, just in case but the given posted var fits perfectly my selection.</p>
<p>So, I read threads suggesting to use Form Events and stuff... But I'm not yet skilled enough and since the Bundle is supposed to work for SF3... Maybe I am missing something?</p>
<hr />
<p><strong>EDIT:</strong></p>
<p>This is my AddressType.php where the form is defined</p>
<pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        -&gt;add('country',        EntityType::class, [
            'label'        =&gt; 'address.country',
            'class'        =&gt; 'MRMyroleBundle:Country',
            'choice_label' =&gt; 'name',
            'query_builder' =&gt; function (CountryRepository $repo) {
                return $repo-&gt;createQueryBuilder('c')
                            -&gt;where('c.actual = 1')
                            -&gt;orderBy('c.cog', 'DESC') // Trick for France first
                            -&gt;addOrderBy('c.name', 'ASC')
                    ;
            },
        ])

         // (...)

        -&gt;add('town',           null,   [
            'label' =&gt; 'address.town',
            'required' =&gt; false,
        ])
        -&gt;add('commune',        Select2EntityType::class, [
            'multiple' =&gt; false,
            'remote_route' =&gt; 'mr_ajax_commune_select',
            'class'        =&gt; 'MRMyroleBundle:Country',
            'primary_key' =&gt; 'id',
            'text_property' =&gt; 'name',
            'minimum_input_length' =&gt; 2,
            'page_limit' =&gt; 30,
            'allow_clear' =&gt; true,
            'delay' =&gt; 250,
            'cache' =&gt; true,
            'cache_timeout' =&gt; 60000, // if 'cache' is true
            'language' =&gt; 'fr',
            'placeholder' =&gt; 'Tapez les premières lettres de votre ville',
            'label'        =&gt; 'address.commune'
        ])

    ;
}
</code></pre>
<p><strong>Controller-side:</strong></p>
<p>The &quot;Address&quot; Entity is a field of the &quot;Production&quot; Entity form:</p>
<pre><code>// Add Production 
public function prodAddAction(Request $request)
{
    $production = new Production();

    $formBuilder = $this-&gt;get('form.factory')-&gt;createBuilder(FormType::class, $production);
    $formBuilder
        -&gt;add('name',           TextType::class, [
            'label' =&gt; 'prod.name'
        ])
        // (...)
        -&gt;add('address',     AddressType::class, [
            'label' =&gt; 'prod.address'
        ])
        -&gt;add('save',           SubmitType::class)
    ;
    $form = $formBuilder-&gt;getForm();


    // Handle POST
    $handleRequest = $form-&gt;handleRequest($request);
    if ($request-&gt;isMethod('POST')){

        if( ! $handleRequest-&gt;isValid()){
            $request-&gt;getSession()-&gt;getFlashBag()-&gt;add('danger', &quot;Une erreur a eu lieu. Merci de vérifier les champs ci-dessous :&quot;);
        }
        else{
            $em = $this-&gt;getDoctrine()-&gt;getManager();
            $em-&gt;persist($production);
            $em-&gt;flush();

            $request-&gt;getSession()-&gt;getFlashBag()-&gt;add('success', 'Votre maison de production a bien été enregistrée.');

            return $this-&gt;redirectToRoute('mr_homepage');
        }
    }

    return $this-&gt;render('ACMEBundle:Prod:prod-add.html.twig', [
        'form' =&gt; $form-&gt;createView()
    ]);
}
</code></pre>
<p>Note: I have no doubt the Ajax returned string is OK since the client-side is processing perfectly, and submitting the correct ID in POST Vars.</p>
<p><strong>EDIT / SOLUTION:</strong></p>
<p>For persons running into same issue, the bundle is innocent: i targeted the wrong Entity class in my &quot;Commune&quot; field - i targeted Country entity instead in AdressType, line 25 in my sample. An odd copy/paste.</p>