A
A
Alexey Alekseev2020-04-20 19:07:12
symfony
Alexey Alekseev, 2020-04-20 19:07:12

How to pass choices values ​​for ChoiceType to Symfony form from controller?

Hello!
I have a ChoiceType in my form. Its values ​​(choices) are entries in the database.
My form is implemented in a separate class:

class FindForm extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ...
            ->add('type',ChoiceType::class, array(
                  'choices'  => array(
                      'Значение_1' => '1',
                      'Значение_2' => '2',
                      'Значение_3' => '3',
                      'Значение_4' => '4',
                  ),
                'required' => false,
                'multiple' => true,
                'expanded' => true
            ))
  ...
            ));
    }


In the controller, I create a form
$form = $this->createForm(FindForm::class);
and select data from the database.
I somehow need to pass this array of data to the form for 'choices' , but
I don't know how to do it. Symfony 3.4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-04-20
@remlin1000

To select entities in forms, there is EntityType:
https://symfony.com/doc/3.4/reference/forms/types/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question