I'm trying to add multiple checkboxes to my form (Symfony Forms, using Symfony 4) by using the ChoiceType
. However when loading the page every single checkbox will be selected by default, which I do not want.
The choices are collected in the controller before the form is rendered and send using the $options
, like this:
$choices = $tableRepository->getChoices();
$form = $this->createForm(FormType::class, [
'choices' => $choices,
]);
Here an actual part of the $choices
as collected:
array(884) {
[111]=>
string(7) "sbi111"
[113]=>
string(7) "sbi113"
And then in the form:
->add('names', ChoiceType::class, [
'label' => 'Choose: ',
'multiple' => true,
'expanded' => true,
'choices' => $options['data']['choices'],
'required' => false,
])
And like I said this returns the options but all with the checked="checked"
attribute. And the strangest part is when I declare the array directly inside the form, so
->add('names', ChoiceType::class, [
'label' => 'Choose: ',
'multiple' => true,
'expanded' => true,
'choices' => [
'choice1' => 'value1',
'choice2' => 'value2',
],
'required' => false,
])
Then it works just fine? So my guess is that issue is with passing the data inside the $options
, but I'm not sure what to do here & did not find a single person with the same problem, so if anyone has any idea please let me know. Thanks.
Aucun commentaire:
Enregistrer un commentaire