Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Select2

Select2

Edit this page

The admin comes with select2 integration. Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

The select2 is enabled on all select form elements by default.

Disable select2

If you don't want to use select2 in your admin, you can disable it in configuration.

1
2
3
4
5
# config/packages/sonata_admin.yaml

sonata_admin:
    options:
        use_select2: false # disable select2

Note

If you disable select2, autocomplete form types will stop working.

Disable select2 on some form elements

To disable select2 on some select form element, set data attribute data-sonata-select2 = "false" to this form element:

1
2
3
4
5
6
7
8
9
10
11
12
use Sonata\AdminBundle\Form\Type\ModelType;

protected function configureFormFields(FormMapper $form): void
{
    $form
        ->add('category', ModelType::class, [
            'attr' => [
                'data-sonata-select2' => 'false'
            ]
        ])
    ;
}

Note

You have to use false as string! "false"!

AllowClear

Select2 parameter allowClear is handled automatically by admin. But if you want to overload the default functionality, you can set data attribute data-sonata-select2-allow-clear="true" to enable allowClear or data-sonata-select2-allow-clear = "false" to disable the allowClear parameter:

1
2
3
4
5
6
7
8
9
10
11
12
use Sonata\AdminBundle\Form\Type\ModelType;

protected function configureFormFields(FormMapper $form): void
{
    $form
        ->add('category', ModelType::class, [
            'attr' => [
                'data-sonata-select2-allow-clear' => 'false'
            ]
        ])
    ;
}

Note

You have to use false as string! "false"!

Minimum results for search

To control the minimum amount of results that are required before the select is searchable you can set the data attribute data-sonata-select2-minimumResultsForSearch. This controls select2's minimumResultsForSearch parameter:

1
2
3
4
5
6
7
8
9
10
11
12
use Sonata\AdminBundle\Form\Type\ModelType;

protected function configureFormFields(FormMapper $form): void
{
    $form
        ->add('category', ModelType::class, [
            'attr' => [
                'data-sonata-select2-minimumResultsForSearch' => '10',
            ]
        ])
    ;
}

Note

By default minimumResultsForSearch will be set to 10

Maximum selection length

To control the maximum amount of results that can be selected, you can set the data attribute data-sonata-select2-maximumSelectionLength. This controls select2's maximumSelectionLength parameter:

1
2
3
4
5
6
7
8
9
10
11
12
use Sonata\AdminBundle\Form\Type\ModelType;

protected function configureFormFields(FormMapper $form): void
{
    $form
        ->add('category', ModelType::class, [
            'attr' => [
                'data-sonata-select2-maximumSelectionLength' => '3',
            ]
        ])
    ;
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Online exam, become Sylius certified today

    Online exam, become Sylius certified today

    The life jacket for your team and your project

    The life jacket for your team and your project

    Version:

    Table of Contents

    • Disable select2
    • Disable select2 on some form elements
    • AllowClear
    • Minimum results for search
    • Maximum selection length

    Symfony footer

    Avatar of Christian Schmidt, a Symfony contributor

    Thanks Christian Schmidt for being a Symfony contributor

    6 commits • 558 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony