我需要将一个表单嵌入到另一个表单中,并按照以下步骤进行操作:
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"
我如何解决这个问题?如何在没有得到这个错误的情况下将第二种形式嵌入第一种形式?
答案 0 :(得分:1)
试试这个:
$builder->add('extra_info', new AddressExtraInfoType(), array('mapped' => false));
您在 Common \ CommonBundle \ Entity \ StandardAddress 中没有字段 extra_info ,因此您必须在表单类型中使用非映射字段
答案 1 :(得分:0)
最后,在再次阅读我的代码后,我发现了错误的位置。在AddressExtraInfo
实体中,我address_extra_info
当然将其更改为extra_info
以解决问题,无论如何,谢谢您的回复