1

找了好几遍还是有问题。。。

我想制作一个动态表格。我想在另一个选择的功能中水合一个选择。这是我的 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,
                        ])
                    )
                ;
            }
        });
    }

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

你知道为什么吗?有没有制作动态表格的最佳方法?

谢谢你的帮助。

亚历克斯

4

0 回答 0