user1601416
user1601416

Reputation: 73

How to make some checkbox options selected by default in ChoiceType Symfony Form Builder?

I am building a checkbox form that has 4 possible choices. What would be the method of making 2 and 3 (Green and Blue) checked by default but leaving the remaining two items unchecked by default?

I believe the "data" item of the $builder array might be the correct answer, but I am not sure how... Any help would be appreciated! Thank you!

$builder->add(
          "What color do you like?",
          ChoiceType::class,
          [
          'label' => "What color do you like?",
          'choices' => [1 => "Red", 2 => "Green", 3 => "Blue", 4=>"Orange"],
          'expanded' => true,
          'multiple' => true,
          'required' => true,
          'help' => "Pick as many as you like!",
      ]
                );

I expect that I should be able to code in something like "2=1,3=1" somewhere to indicate that both of my choices should be checked by default, but it is not clear how I would indicate that those two choices should be checked by default.

Upvotes: 2

Views: 3765

Answers (2)

Allart
Allart

Reputation: 928

ChoiceType functionality can sometimes be (or seem) quite weird and can be hard to work with. It took me some time to figure out how to set default values. Here is how I did it.

DaysType.php

->add('days', ChoiceType::class, [
    'multiple' => true,
    'expanded' => true,
    'choices' => [
        "Mo" => "Mo",
        "Tu" => "Tu",
        "We" => "We",
        "Th" => "Th",
        "Fr" => "Fr",
        "Sa" => "Sa",
        "Su" => "Su",
    ],
])

Be aware that the first "Mo" before the => is the title/label you will see next to your checkbox. Like you can see in my picture.

enter image description here

The "Mo" after the => is the data that will be send back to your controller once the user submits the form. Its also the value you can see on your input field its value attribute.

Now how to make them checked by default:

DaysController.php

$days = new Days();

// Used to set the default selected values. Options are: Ma, Di, Wo, Do, Vr, Za, Zo.
$days->setDays([
    "Mo", "Tu", "We", "Sa"
]);

// Create form
$form = $this->createForm(DaysType::class, $days);
$form->handleRequest($request);

// ... render page etc.

So now by default Mo, Tu, We and Sa are checked.

This is how the data looks like when the user submits the form without changing the checks:

-days: array:4 [▼
    0 => "Mo"
    1 => "Tu"
    2 => "We"
    3 => "Sa"
]

I do not know how to get an array returned with all possibilities. And with true/false but this works for me. I will probably json_encode the array and save it like that in the database.

Upvotes: 0

msg
msg

Reputation: 8171

First off, the array passed to choices should be in the form label => value, so your values and keys have to be flipped.

Then to add attributes to specific choices you can use the choice_attr option:

'choice_attr' => function($opt, $k, $v) {
    if ($v == '2' || $v == '3') {
        return ['checked' => 'checked'];
    }
},

Upvotes: 4

Related Questions