Forms 如何将数据从formType父级传递到formType子级

Forms 如何将数据从formType父级传递到formType子级,forms,symfony,conditional-statements,entity-relationship,Forms,Symfony,Conditional Statements,Entity Relationship,我的表格分为几个阶段,包括: 步骤1:基于实体1的简单表单,记录数据集的第一部分。 步骤2:父表单(基于步骤1中的实体1),它实现了子表单(基于实体2)=>CollectionType。父窗体($builder)包含步骤1中的数据,但不包含子窗体 我的目标很简单:我的子表单包含一个ChoiceType,它必须根据给定的 我的问题:将父窗体拥有的一段数据($builder->getData())传递给子窗体,以便为我的ChoiceType设置选项条件 我寻找解决办法,但没有找到任何结果。所以我认为

我的表格分为几个阶段,包括:

步骤1:基于实体1的简单表单,记录数据集的第一部分。 步骤2:父表单(基于步骤1中的实体1),它实现了子表单(基于实体2)=>CollectionType。父窗体($builder)包含步骤1中的数据,但不包含子窗体

我的目标很简单:我的子表单包含一个ChoiceType,它必须根据给定的

我的问题:将父窗体拥有的一段数据($builder->getData())传递给子窗体,以便为我的ChoiceType设置选项条件

我寻找解决办法,但没有找到任何结果。所以我认为我的问题不是一个bug,而是一个糟糕的方法

我测试了:

//PARENT
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('wans', CollectionType::class,
                [
                    'entry_type' => WanDType::class,
                    'entry_options' => [
                        'label' => false,
                        'data' => $builder->getData() // <- here
                    ],
                ]);
    }

//ENFANT
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $ma_valeur = $builder->getData()->getValue(); //<- here
        if(in_array($ma_valeur, ['1'])) { //<- here
            $builder->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        } else { //<- here
            $builder->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        } //<- here
    }

我认为您希望做这样的事情可以提供:


这样,您就可以根据提供的数据更改表单。我觉得这不是很直观。当想要影响集合所包含的实体时,没有任何东西可以解释如何在集合的上下文中理解此技术

因此,我们不关心父母和子女形式的概念。它只干预受修改影响的表单(如果是子表单,则它将位于formType子表单中)

基本上,这就是:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add([....]); // without the fields I want to modulate.

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event){
        $wan = $event->getData();

        if(in_array($wan->getEquipment()->->getValue(),['1'])) {
            $event->getForm()->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        } else {
            $event->getForm()->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        }
    });
}

谢谢你的建议。我看过这个文档。只有她用了一个简单的形式。在我之外,我想用父窗体包含的内容影响嵌套的子窗体。我不知道如何将这些解决方案应用到我的需要中:/可能是通过
$event->getForm()->getParent()->getData()
我明白了。但是如何将我在EquipmentDType(父级)中生成的$event传递给WanDType(子级)?问题仍然是一样的,对吗?我认为您只需要在子窗体中添加事件,而不需要在父窗体中使用它。好。在我的孩子表格(WanDType)中,我进行了测试。我在主帖上写了“编辑测试1”。我可能做错了,因为它不起作用。他给我看了ok2,没有给我看其余的。
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add([....]); // without the fields I want to modulate.

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event){
        $wan = $event->getData();

        if(in_array($wan->getEquipment()->->getValue(),['1'])) {
            $event->getForm()->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        } else {
            $event->getForm()->add('profil', ChoiceType::class, [
                'choices' => [
                    [...]
                ],
                'label' => false,
            ]);
        }
    });
}