Skip to content

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"!

AllowTags

Select2 parameter allowTags can be set using the data attribute data-sonata-select2-allow-tags="true" to enable allowTags:

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-tags' => 'true'
            ]
        ])
    ;
}

Note

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

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