tetranz / select2entity-bundle

A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.
MIT License
218 stars 110 forks source link

Can't get the search input #91

Open marvin-SL opened 7 years ago

marvin-SL commented 7 years ago

hello,

i'm using SF3 and tried to install this bundle. I'm supposed to get an input form with a "search" input text like https://github.com/tetranz/select2entity-bundle/blob/master/Resources/doc/img/multi.png but I've got this : 1

Here is my form :

use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
...
  public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('recipient', Select2EntityType::class, [
            'multiple' => true,
            'remote_route' => 'user_select',
            'class' => User::class,
            'primary_key' => 'id',
            'text_property' => 'name',
            'minimum_input_length' => 2,
            'page_limit' => 10,
            'allow_clear' => true,
            'delay' => 250,
            'cache' => true,
            'cache_timeout' => 60000, // if 'cache' is true
            'language' => 'en',
            'placeholder' => 'Select a country',
            'label' => 'recipient',
            'translation_domain' => 'FOSMessageBundle',
            // 'object_manager' => $objectManager, // inject a custom object / entity manager
        ])
            ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array(
                'label' => 'subject',
                'translation_domain' => 'FOSMessageBundle',
            ))
            ->add('body', FroalaEditorType::class, array(
                'label' => 'body',
                'translation_domain' => 'FOSMessageBundle',
            ));
    }

My Appkernel contains :

            new Braincrafted\Bundle\BootstrapBundle\BraincraftedBootstrapBundle(),
            new Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle(),

My template:

({% block stylesheets %}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="{{ asset('node_modules/select2-bootstrap-css/select2-bootstrap.min.css') }}"></script>
{% endblock %}
<h2>{% trans from 'FOSMessageBundle' %}send_new{% endtrans %}</h2>

<form action="{{ url('message_thread_new') }}" method="post">
    {{ form_widget(form) }}

    <input type="submit" />
</form>

{% endblock %}

{% block javascripts %}

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
{% endblock %}))

My controller :


    /**
     * @Route("/query/user.json", name="user_select")
     */
    public function userSelectAction(Request $request)
    {
      $em = $this->getDoctrine()->getManager();

        $result =  $em->createQuery('
            SELECT u.id, u.username as text
            FROM AppBundle:User u
            WHERE lower(u.username)  LIKE :username
            ORDER BY u.username
        ')
        ->setParameter('username', '%' . $query . '%')
        ->setMaxResults(10)
        ->getArrayResult();

        return new JsonResponse($result);
    }

Any idea ? Thanks

1gorsh commented 6 years ago

Have faced with the same problem, downgrading select2.js from 4.0.6 to 4.0.1 solved the problem.

tetranz commented 6 years ago

I haven't been able to reproduce this. It works for me with select2 4.0.6-rc.0.