奏鸣曲管理员和动态表格

时间:2015-06-09 08:26:11

标签: symfony symfony-sonata sonata

我做了几次搜索,但我仍有问题...

我想创建一个动态表单。我想在其他选择的功能中补充一个选择。 这是我的configureFormFields:

protected function configureFormFields(FormMapper $formMapper)
    {
        $emIndustry = $this->modelManager
                           ->getEntityManager('*\*\*\*\Entity\Industry')
        ;

        $query = $emIndustry->getRepository(*:Industry')
                            ->getFindAllParentsQueryBuilder()
        ;

        $formMapper
            ->add('company')
            ->add('industry', 'sonata_type_model', [
                'attr' => [
                    'onchange' => 'submit()',
                ],
                'query'    => $query,
                'required' => false,
            ])
            ->add('subIndustry', 'sonata_type_model', [
                'choices' => [],
                'required' => false,
            ])
        ;

        $builder = $formMapper->getFormBuilder();
        $factory = $builder->getFormFactory();
        $subject = $this->getSubject();
        $modelManager = $this->getModelManager();

        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use($formMapper, $subject, $emIndustry, $modelManager, $factory) {
            $form = $event->getForm();

            if(!is_null($subject->getIndustry())) {
                $query = $emIndustry->getRepository('*:Industry')
                    ->getFindChildrenByParentQueryBuilder($subject->getIndustry())
                ;

                $form
                    ->add(
                        $factory->createNamed('subIndustry', 'sonata_type_model', null, [
                            'class'    => '*\*\*\*\Entity\Industry',
                            'query'    => $query,
                            'required' => false,
                        ])
                    )
                ;
            }
        });
    }

当我更改select Industry的值时,我的表单没有问题。但是在第二个选择子行业中没有任何事情发生,因为:我的$ subject对象的所有属性都是null ...

你知道为什么吗?有没有最好的方式来制作动态表格?

感谢您的帮助。

AlexL

0 个答案:

没有答案