My problem was that I did not add the new tag as the <option> in my own select box.
This is necessary because select2 checks the values ββset in trough select2.val(values) if the <option> with this value exists. If select2 has not selected an extra value from the array and sets an array of values ββthat have the corresponding parameter tag in the base selection field.
So this is how it works now (for select2 4.0.x):
$('select.tags').select2({ tags: true, ajax: { url: '{{ path('tag_auto_complete') }}', processResults: function (data) { return { results: data.items, pagination: { more: false } }; } }, createTag: function (tag) { return { id: tag.term, text: tag.term, tag: true }; } }).on('select2:select', function (evt) { if(evt.params.data.tag == false) { return; } var select2Element = $(this); $.post('{{ path('tag_crrate_auto_complete') }}', { name: evt.params.data.text }, function( data ) {
The minimum AJAX response (JSON format) should look like this:
[ {'id': '1', 'text': 'foo'}, {'id': '2', 'text': 'bar'}, {'id': '3', 'text': 'baz'} ]
You can add additional data to each result to allow yourself to render a list of results with additional data in it.
source share