Installation
composer require tetranz/select2entity-bundle
Add to config/bundles.php:
return [
// ...
Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle::class => ['all' => true],
];
Basic Usage in a Form
use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
$builder->add('user', Select2EntityType::class, [
'class' => User::class,
'property' => 'fullName', // Display property
'multiple' => false,
]);
First Use Case
Replace a standard EntityType field in a form with Select2EntityType for an enhanced UX with search/filtering.
Basic Entity Selection
$builder->add('role', Select2EntityType::class, [
'class' => Role::class,
'property' => 'title',
]);
Multiple Selection
$builder->add('tags', Select2EntityType::class, [
'class' => Tag::class,
'property' => 'name',
'multiple' => true,
]);
Custom Query Builder
$builder->add('project', Select2EntityType::class, [
'class' => Project::class,
'query_builder' => function (EntityManagerInterface $em) {
return $em->getRepository(Project::class)
->createQueryBuilder('p')
->where('p.isActive = :active')
->setParameter('active', true);
},
'property' => 'name',
]);
Integration with Symfony Forms
FormBuilder and FormType classes.required, label, attr).Twig Integration
{{ form_row(form.user) }}
Ensure select2 JavaScript is loaded (see Gotchas).
Dynamic Dependencies
Use onChange events to dynamically update other fields:
$('#form_user').on('change', function(e) {
const userId = $(this).val();
// Fetch related data via AJAX
});
Custom Templates
Override the default template in templates/TetranzSelect2EntityBundle/form/fields.html.twig.
Lazy Loading
Use ajax configuration for large datasets:
$builder->add('user', Select2EntityType::class, [
'ajax' => [
'url' => $this->generateUrl('app_user_autocomplete'),
'data' => ['id' => 'js-user-id'],
],
]);
Validation
Combine with Symfony validators (e.g., @Assert\Valid or custom constraints).
JavaScript Dependencies
select2 JS/CSS is loaded after the form field:
{{ parent() }}
{{ form_widget(form) }}
{{ include('TetranzSelect2EntityBundle::select2.js') }}
import 'select2';
Entity Manager Injection
query_builder, ensure the EntityManagerInterface is injected into your form type or service.Multiple Fields with Same Class
attr options (e.g., attr => ['class' => 'select2-user']) to avoid conflicts.Translation Issues
# messages.en.yml
tetranz_select2_entity.search: "Search users..."
Performance with Large Datasets
ajax mode or query_builder with pagination/filtering:
'query_builder' => function (EntityManagerInterface $em) {
return $em->getRepository(User::class)
->createQueryBuilder('u')
->setMaxResults(50);
},
Check Console for Errors
Uncaught ReferenceError: Select2 is not defined (missing JS).data-select2-entity attributes are rendered in HTML.Inspect Network Requests
ajax mode, check if the endpoint returns valid JSON:
[
{"id": 1, "text": "John Doe"},
{"id": 2, "text": "Jane Smith"}
]
Clear Cache
php bin/console cache:clear
If templates or configurations aren’t updating.
Customize Select2 Options
Pass options via select2_options:
$builder->add('user', Select2EntityType::class, [
'select2_options' => [
'width' => '100%',
'placeholder' => 'Select a user...',
'minimumInputLength' => 2,
],
]);
Override Templates
Copy templates/TetranzSelect2EntityBundle/form/fields.html.twig to your bundle’s Resources/views/ and customize.
Add Custom Data Attributes
$builder->add('user', Select2EntityType::class, [
'attr' => ['data-custom' => 'value'],
]);
Event Listeners
Attach JavaScript events via attr:
$builder->add('user', Select2EntityType::class, [
'attr' => [
'data-on-select' => 'handleUserSelect',
],
]);
How can I help you explore Laravel packages today?