Skip to content

Commit

Permalink
Added support for select2 tags
Browse files Browse the repository at this point in the history
  • Loading branch information
micbis committed Oct 30, 2024
1 parent 672a614 commit c165317
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const Admin = {
const popover = select.data('popover');
let maximumSelectionLength = null;
let minimumResultsForSearch = 10;
let allowTags = false;

select.removeClass('form-control');

Expand All @@ -101,6 +102,12 @@ const Admin = {
allowClearEnabled = false;
}

if (select.attr('data-sonata-select2-allow-tags') === 'false') {
allowTags = false;
} else {
allowTags = true;
}

if (select.attr('data-sonata-select2-maximumSelectionLength')) {
maximumSelectionLength = select.attr('data-sonata-select2-maximumSelectionLength');
}
Expand All @@ -117,6 +124,7 @@ const Admin = {
placeholder: allowClearEnabled ? ' ' : '', // allowClear needs placeholder to work properly
allowClear: allowClearEnabled,
maximumSelectionLength,
tags: allowTags,
});

if (undefined !== popover) {
Expand Down
23 changes: 23 additions & 0 deletions docs/cookbook/recipe_select2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ to enable ``allowClear`` or ``data-sonata-select2-allow-clear = "false"`` to dis

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``::

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 false as string! ``"false"``!

Minimum results for search
--------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/app.js

Large diffs are not rendered by default.

0 comments on commit c165317

Please sign in to comment.