How to Integrate Select2 JS in Symfony 3
<p>I will show you how to integrate <code>Select2</code> in a Symfony3 project!</p>
<p>First, Add those 3 links to <code>base.html.twig</code> page.</p>
<pre><code>&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link href =&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Second, In <code>CompanyType.php</code> add:</p>
<pre><code> use Symfony\Bridge\Doctrine\Form\Type\EntityType;

 class CompanyType extends AbstractType  {
     /**
      * @param FormBuilderInterface $builder
      * @param array $options
      */
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
  $builder
        -&gt;add('country',EntityType::class, array(
                 'attr'=&gt;array('class'=&gt;&quot;country&quot;,),
                 'label'=&gt;'Country of The Head fice',                     

                  'choices_as_values' =&gt; true, //               
                 'class' =&gt; 'QSCORBundle\Entity\CountryMaps',
                 'choice_label' =&gt; 'Country_Name',
 
             ))

    -&gt;add(...)  
}
  ...
 }
</code></pre>
<p>after that, on ur Entity file <code>Country.php</code> add:</p>
<pre><code>public function __toString()
    {
        // TODO: Implement __toString() method.

       return $this-&gt;getCountryName();
    }
</code></pre>
<p>Finally, in your template file <code>xxx.html.twig</code> write this:</p>
<pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
                $(document).ready(function() {
                        $(&quot;.country&quot;).select2();
                });
&lt;/script&gt;
{{ form_widget(form.country)}}
</code></pre>
<p>Here is an image of how it looks like:</p>
<p><a href="https://i.stack.imgur.com/CJL3Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CJL3Q.png" alt="enter image description here" /></a></p>