Search code examples
symfonyjquery-select2symfony-3.4symfony-3.3symfony3.x

Allow spaces when using Select2 tags symfony


I try to create tags if not exist by using select2 entity. When I try to add a tag that contains spaces I can't. For example :

  1. "Yupi yola" --> not work because tag contains spaces, is getting only first word "Yupi".
  2. "Test" ---> work

  3. "Test_test" ---> work

I added the Select2Entity in my form type

->add('groupe', Select2EntityType::class, [
      'remote_route' => 'route',
      'class' => Class::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 group',
      'allow_add' => array(
           'enabled' => true,
           'new_tag_text' => '',
           'tokenSeparators'=> '[",", " "]'
      ),
      'multiple'             => false,
      'attr'                 => ['style' => 'width:100%'],
      'scroll'               => true,
 ])

Please help me. Thx in advance.


Solution

  • Instead of 'tokenSeparators', use 'tag_separators'

    'allow_add' => array(
               'enabled' => true,
               'new_tag_text' => '',
               'new_tag_prefix' => '__',
               'tag_separators'=> '[",", " "]'
          )