pinano/select2-bundle
Symfony2 bundle that packages Select2 (v4.0.3) assets for easy use in Twig/Assetic. Install via Composer, register the bundle, run assets:install, then include select2 JS/CSS plus optional i18n locale files.
Installation:
composer require pinano/select2-bundle
php app/console assets:install web
Ensure PinanoSelect2Bundle is registered in AppKernel.php.
Basic Twig Integration: Include the Select2 JS and CSS in your base template:
{% block stylesheets %}
{{ parent() }}
{{ asset('bundles/pinano/select2/css/select2.css') }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
{{ asset('bundles/pinano/select2/js/select2.js') }}
{{ asset('bundles/pinano/select2/js/i18n/en.js') }} {# Optional: Language support #}
{% endblock %}
First Use Case:
Convert a standard <select> into a Select2-enhanced dropdown:
{{ form_widget(form.field) }}
<script>
$(document).ready(function() {
$('select').select2();
});
</script>
Form Integration: Use with Symfony Forms for dynamic dropdowns:
{{ form_row(form.country) }}
<script>
$('#{{ form.country.vars.id }}').select2({
placeholder: "Select a country",
allowClear: true
});
</script>
AJAX Data Loading: Fetch remote data dynamically (e.g., from an API):
<select class="js-data-select"></select>
<script>
$('.js-data-select').select2({
ajax: {
url: '{{ path('app_api_countries') }}',
dataType: 'json',
delay: 250,
data: function(params) {
return { term: params.term };
},
processResults: function(data) {
return { results: data };
}
}
});
</script>
Tagging/Tokenization: Enable free-form input with tags:
<select class="js-tags"></select>
<script>
$('.js-tags').select2({
tags: true,
tokenSeparators: [',', ' ']
});
</script>
Integration with Form Events: Initialize Select2 after form submission (e.g., via AJAX):
$('#my-form').on('submit', function(e) {
e.preventDefault();
$.post($(this).attr('action'), $(this).serialize(), function() {
$('select').select2(); // Reinitialize after dynamic updates
});
});
config.yml:
pinano_select2:
assets:
js: 'bundles/pinano/select2/js/custom-select2.js' # Custom JS file
css: 'bundles/pinano/select2/css/custom-select2.css'
{% block pinano_select2_styles %} and {% block pinano_select2_scripts %} in your base template for cleaner asset handling.JQuery Dependency:
{{ asset('js/jquery.js') }} {# Must come first #}
{{ asset('bundles/pinano/select2/js/select2.js') }}
Asset Paths:
/bundles/pinano/...) breaks in production if assets:install isn’t run.asset() or url() helpers:
{{ asset('bundles/pinano/select2/js/select2.js') }}
Language Files:
select2.js:123 Uncaught TypeError).i18n/*.js file after select2.js.Form Theme Conflicts:
{% block select_widget %}
{{ block('widget_container') }}
<script>
$(document).ready(function() {
$('{{ select|e('js') }}').select2({{ options|raw }});
});
</script>
{% endblock %}
Dynamic DOM Elements:
$(document).on('change', '.js-dynamic-select', function() {
$(this).select2();
});
php app/console assets:install --symlink web --verbose
data-select2 Attribute: For debugging, add data-select2 to elements to force initialization:
<select data-select2></select>
<script>
$('[data-select2]').select2();
</script>
Custom Templates: Override Select2’s templates (e.g., for custom dropdown items):
$.fn.select2.amd.require(['select2/data/select'], function(SelectData) {
function CustomData($element, options) {
CustomData.__super__.constructor.call(this, $element, options);
}
CustomData.prototype = Object.create(SelectData.prototype);
$.fn.select2.SelectData = CustomData;
});
Event Handling: Listen to Select2 events for dynamic behavior:
$('.js-select').on('select2:select', function(e) {
console.log('Selected:', e.params.data.id);
});
Server-Side Processing:
Use Symfony’s FormEvent to preprocess data for Select2 AJAX:
// src/AppBundle/EventListener/FormListener.php
public function onPreSetData(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
if ($form->getName() === 'country_form') {
$form->add('countries', 'entity', [
'class' => 'AppBundle:Country',
'property' => 'name',
'query_builder' => function($repo) use ($data) {
return $repo->createQueryBuilder('c')
->where('c.region = :region')
->setParameter('region', $data->getRegion());
},
]);
}
}
How can I help you explore Laravel packages today?