I am having difficulty creating different placeholders for each form field that work with Select2. My first placeholder is also displayed on any consecutive field, which should have it's own placeholder. A similar question might have been asked before, although I do not completely understand that question, nor does it have an answer.
What I have so far
On my file base.html.twig
I included the script of Select2 and have created these few lines of code:
<script>
$('select[data-select="true"]').select2({
placeholder: $('select[data-select="true"]').attr("data-placeholder")
});
</script>
In my FormType file, in the form builder I have:
->add('firstEntity', EntityType::class, [
'class' => firstEntity::class,
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'first placeholder']
])
->add('secondEntity', EntityType::class, [
'class' => secondEntity::class,
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'second placeholder']
])
The problem is that in a form with two fields like above, the first placeholder is displayed in both fields. So 'first placeholder'
will be the placeholder for both fields instead of both fields having their own.
My question: how can I have individual placeholders for each Select2 form field?