S
S
symnoob2021-04-20 19:20:53
symfony
symnoob, 2021-04-20 19:20:53

How to create a checkbox form from an array of objects?

Hello everyone, I can not understand how the forms work.
I'm trying to create a form that would generate a checkbox sheet.

The data comes from the repository:

$paginatedUsers = $userRepository->getAll($paginated, $userArt, $this->getUser())->getQuery()->getResult();
//простой массив с объектами
[
  0 => App\Entity\User\User
  1 => App\Entity\User\User
  2 => App\Entity\User\User
]


parent form type:
$builder
            ->add('user', CollectionType::class, [
                'entry_type' => ListType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'label' => false,
                'by_reference' => false
            ])


well, here's the ListType:
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', CheckboxType::class, [
                'label' => false,
            ])

        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }


The form is empty :-(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sl0, 2021-04-20
@symnoob

If I understood the question correctly, then you need something like

$builder
            ->add('user', ChoiceType::class, [
                'label' => false,
                'multiple' => true,
                'expanded' => true,
                'choices' => [
                 /// вот тут и должен быть массив с вариантами
                ],
            ])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question