Hello, i’m connected to a movie API with a select2. Using Symfony 4.3.3.
I want to give to my controller some specific value from the JSON (given by the api).
The problemen is that when i select a movie with select2, he set the value with the id of the film and I don’t know why and i don’t know how to change that.
<option value ="13>Forrest Gump</option>
This is what is given to my controller. And 13
is the id of the movie “Forrest Gump”.
Here is my code :
$(document).ready(function () {
$(".products-select2-num").select2({
language: 'fr-FR',
width: '100%',
closeOnSelect: false,
placeholder: 'Entrez le nom du film, de la série ou de la personne.',
minimumInputLength: 3,
ajax: {
url: function (params) {
return 'https://api.exemplefilms.org/3/search/multi?api_key=monapicachee&language=fr&query='+params.term+'&page=1&include_adult=false';
},
dataType: 'json',
delay: 250,
processResults: function (data) {
var results = [];
data = data.results;
data.forEach(e => {
results.push({ id: e.id, text: e.title||e.name, media: e.media_type });
});
return {
results: results
};
},
data: function (query) {
return query;
},
},
templateResult: formatResult,
});
function formatResult(d) {
$d = $('<option/>').val(d.media).text(d.text);
return $d;
}
});
I would like to give like I said before some other things as value.
thank you for your help.