On an application I created a form with a field (select) in EntityType, I then passed it to select2, no problem there.
The twig view looks like an array of all saved helpers and you can modify a helper by opening a modal, I correctly pass my value twig to js with dataset and all the fields are filled in correctly.
To better have a little visual, my button that opens my modal and sends it all the info looks like this :
<a href="javascript:;" class="menu-link px-3" {{ stimulus_action('deal--handle', 'editHelp') }} data-url2="{{ path('help_request_edit', {'id':help.id}) }}" data-organisme="{{ help.organization.value }}" data-date="{{ help.date|date('Y-m-d') }}" data-state="{{ help.state }}" data-amount-requested="{{ help.amountRequested }}" data-amount-to-deduct="{{ help.amountToDeduct }}" data-subrogation="{{ help.subrogation }}" data-quotations="{{ quotationInHelp|json_encode }}">
Modify
</a>
data-quotations="{{ quotationInHelp|json_encode }}" send the data to js and I get it like this:
var quotations = JSON.parse(event.currentTarget.dataset.quotations);
in my console.log(quotations) everything is ok.
but I can't find how to use the data of my "quotations" variable to pass them into values of my select2, for example the other fields of my modal are filled like this:
this.subrogationTarget.value = event.currentTarget.dataset.subrogation;
if you have any ideas/solutions for this problem, thank you!
json_encodedoesn't mark the output as safe, as seen here. You need to add the filterrawas well#[ORM\ManyToMany(targetEntity: Quotation::class, inversedBy: 'helpRequests')] private Collection $quotations;` {% set quotationInHelp = [] %} quotationsInHelp contains an array of my entity(ies) linked to a helpRequest and it is this "quotationsInHelp" that I want to pass to js, an array with one/several entities