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

时间:2019-04-17 08:17:10

标签: forms symfony conditional-statements entity-relationship

我有一个分几个阶段进行的表格,包括:

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

我的对象很简单:我的子窗体包含一个ChoiceType,它必须根据给定的值而变化

我的问题:将父表单具有的数据块($ builder-> getData())传递给子表单,以为我的ChoiceType设置选项条件。

我在寻找解决方案,但未找到任何结果。所以我认为我的问题不是错误,而是我的错误方法。

我测试过:

//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
    }

=>我收到一条错误消息,说明我的data_class与子窗体不匹配。实际上,父表单的data_class替换了子表单的数据。该错误建议赋予它空值,但会导致其他错误,并且symfony越来越不高兴(我认为这是逻辑上的,不是一个好习惯)。

=>我尝试使用$ options。 Symfony不满意,因为他拒绝添加起点不知道的key => values值。

=>我考虑过要以父表格的形式提出条件。但是条件会随着时间而变化,因此我认为按条件类型创建子表单并不是一种好习惯。

=>我还考虑过直接在视图中创建此字段。但这也不是最佳选择。

=>我测试了一些没有用到的东西,每次都会产生负面结果。

我强烈怀疑SensioLabs没有想到这种事情。我可能不必走太远,如果有人知道如何解释,那就太好了。

感谢您的帮助:)

================================================ =====

编辑测试1

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        dump($event->getForm()->getParent()->getData());
        die('ok');
    });
    die('ok2');
}

2 个答案:

答案 0 :(得分:1)

所以。我觉得这不是很直观。当人们想要影响集合中包含的实体时,没有什么可以解释在集合的上下文中必须如何理解这一技术的。

因此,我们不在乎父子形式的概念。只是干预受我们的修改影响的表单(如果是子表单,它将在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,
            ]);
        }
    });
}

答案 1 :(得分:0)

我认为您正在寻找可以提供以下内容的方法:https://symfony.com/doc/current/form/dynamic_form_modification.html

您将可以根据提供的数据更改表格

相关问题