How to save a list of checkboxes to an entity using ChoiceType or EntityType in Symfony forms 6.3

25 Views Asked by At

I am developing a web system to manage soccer academies, for this I must register a player and assign preferred playing positions, one or more can be selected through checkboxes.

My case is the following:

In the Player entity I have created a field principal_positions

#[ORM\Column(length: 255, nullable: true)]
private ?string $principal_positions = null;

Which will receive the positions of the player_position entity

I have created an EntityType field to display the checkboxes as follows

->add('principal_positions', EntityType::class, [
            'class' => PlayerPosition::class,
            'choice_value' => 'label',
            'choice_label' => 'label',
            'expanded'  =>  true,
            'multiple'  =>  true,
            'required'  =>  true,
            'label_attr' => [
                'class' => 'form-check-label'
            ]
        ])

When entering the form everything is displayed perfectly, but when the form is submitted, garbage is saved

Doctrine\Common\Collections\ArrayCollection@000000000000087f0000000000000000

So the questions I ask are:

  1. What is the best strategy to implement multiple option selection using ChoiceType or EntityType checkboxes?
  2. What is the correct way to display and store in the player entity the list of positions using ChoiceType?
  3. What is the correct way to display and store in the player entity the position list using EntityType?

Thanks

0

There are 0 best solutions below