Symfony 表单与子表单

7
我有一个用户表单和一个联系表单,在我的用户表单中,我尝试将我的联系表单添加到用户表单中。
当我尝试添加时。
$builder->add(
            'contact',
            new ContactType()
        );

它失败了。
You cannot add children to a simple form.
Maybe you should set the option "compound" to true?

尝试设置化合物,但没有成功。
/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $defaults = array(
        'compound' => true,
        'inherit_data' => true,
    );

    $resolver->setDefaults($defaults);
}

3
联系人属于用户吗?它与用户有关系吗?如果答案是否定的,您需要添加 $builder->add( 'contact', new ContactType(), array('mapped' => false) ); - Fernando Caraballo
它有一个关联,但不小心覆盖了getParent。 - Alexander Schranz
1个回答

14

compound选项默认设置为true

  1. 你是如何扩展你的表单类型类的?使用了AbstractFormType还是其他什么?
  2. 你是否覆盖了getParent()方法?

这可能解释了compound被设置为false的原因。


非常感谢!我本以为已经删除了父级,但是忘记在自己的抽象类型中删除自定义的getParent。 - Alexander Schranz
太好了 :) 很高兴能帮忙 :) - Jovan Perovic
2
如果提出关于扩展的问题,可以加1分。我本应该继承AbstractType类,但不小心继承了错误的类,导致出现了原始问题中的错误提示。 - undefinedman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接