繁体   English   中英

将表单嵌入到另一个链接到Symfony2中实体的表单中

[英]Embed form into another form linked to entity in Symfony2

我需要将一种形式嵌入另一种形式中,我正在做如下工作:

use Symfony\Component\Form\AbstractType,
    Symfony\Component\Form\FormBuilderInterface,
    Symfony\Component\OptionsResolver\OptionsResolverInterface,
    Common\CommonBundle\Form\AddressExtraInfoType;

class StandardAddressType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('country', 'entity', array( ... ))
                ->add('state', 'entity', array( ... ))
                ->add('city', 'entity', array( ... ))
                ->add('extra_info', new AddressExtraInfoType());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Common\CommonBundle\Entity\StandardAddress'
        ));
    }

    public function getName() {
        return 'common_commonbundle_standard_address';
    }

}

由于主表单需要附加到'data_class' => 'Common\\CommonBundle\\Entity\\StandardAddress'所以当我尝试获取表单时会出现此错误:

Neither the property "extra_info" nor one of the methods "getExtraInfo()", "isExtraInfo()", "hasExtraInfo()", "__get()" exist and have public access in class "Common\CommonBundle\Entity\StandardAddress"

我该如何解决? 如何在不弄错的情况下将第二种形式嵌入第一种形式?

尝试这个:

$builder->add('extra_info', new AddressExtraInfoType(), array('mapped' => false));

in class so you must use non mapped field in form type 您在类没有字段 ,因此您必须使用表单类型的非映射字段

最后,再次阅读我的代码后,我发现了错误所在。 在我的AddressExtraInfo实体中,我有address_extra_info ,当然可以将其更改为extra_info解决此问题,无论如何,感谢您的答复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM