symfony choicetype multiple display selected values

  Kiến thức lập trình

I’m stuck on a problem with Symfony 7 with choicetype using Mulitple mode.

There is something weird,

I’got an array including groups for choices.

->add('Groups', ChoiceType::class, ["attr" => ["class" => "form-control shadow-none pt-2", "multiple" => true ], "label" => "User Mgr. Group", 'help' => 'Select one or multiple Groups.', 'data' => ['Test Group', 'All Users'] ,'choices' => $gps])

$gps being an array like this: ["All Users" => "1", "Test Group" => "2" ].

If I set it up like this, I’ve got a Warning: Array to string conversion
However, if the array is: ["All Users" => true, "Test Group" => true ], then I’m displaying the list (Weird).

My goal is to display a group list with their index as value.So, If I select all groups, the selectbox should send all.

When I receive the data, I need to display this when the user is in multiple groups like this..

enter image description here

I used : 'data' => ['Test Groups', 'All Users'] but whatever I set for tests, no way.

What’s the good practice to do that please?

I tried to read the Symfony document and some threads here, but not found.
I already used choicetype in my form, that work with simple code (not multiple) and with an array like this:

'choices'  => [
            'Yes' => "yes",
            'No' => "no",
        ],

I don’t understand why it wont works.
I don’t have much experiences on Symfony sorry.

LEAVE A COMMENT