Access to the collection form field from the controller in Symfony2

I am creating a form that displays from two different type classes in Symfony2 (using the collection type for the second type), and I am having problems accessing data from the collection field in the controller. Here is the code for the external formBuilders method:

// ...
class EmployeeCreateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('positions', 'collection', array(
                'type' => new PositionCreateType(),
                'label' => ' ',
                'allow_add' => false,
                'prototype' => false,
            ));
    }
// ...

and here is the code for the internal buildForm method from PositionCreateType:

   // ...
    class PositionCreateType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'choice', array(
                'label' => 'Title: ',
                'choices' => array(
                    'Senior Engineer',
                    'Staff',
                    'Engineer',
                    'Senior Staff',
                    'Assistant Engineer',
                    'Technique Leader',
                ),
                'expanded' => true,
                'multiple' => false,
            ))
            ->add('department', 'choice', array(
                'label' => 'Department: ',
                'choices' => array(
                    'd001' => 'Marketing',
                    'd002' => 'Finance',
                    'd003' => 'Human Resources',
                    'd004' => 'Production',
                    'd005' => 'Development',
                    'd006' => 'Quality Management',
                    'd007' => 'Sales',
                    'd008' => 'Research',
                    'd009' => 'Customer Service',
                ),
                    'expanded' => true,
                    'multiple' => false,
            ));
    }
    // ...

I would like to access the department field from my controller, but I cannot figure out how to do this. I tried to do something like

$form->get('positions')->get('department')->getData();

but it does not work.

+4
source share
1 answer

. ArrayCollection, , , , . ( ), :

$form->get('positions')->getData()->get('0')->getDepartment();

,

$form->get('positions')->getData()->get('0')

(), , PositionCreateType().

+3

Source: https://habr.com/ru/post/1526986/


All Articles