我有这张表格:
OrdersType
class OrdersType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Others $builder fields goes here
if ($this->register_type[0] == "natural")
{
$builder->add('person', new NaturalPersonType(), array('label' => FALSE));
}
elseif ($this->register_type[0] == "legal")
{
$builder->add('person', new LegalPersonType(), array('label' => FALSE));
}
}
}
PersonType
class PersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', 'text', array(
'required' => TRUE,
'label' => FALSE
))
->add('contact_person', 'text', array(
'required' => FALSE,
'label' => 'Persona de Contacto'
));
}
}
这就是我在控制器中所做的事情:
public function editAction($id = null)
{
$em = $this->getDoctrine()->getManager();
$order = $em->getRepository('FrontendBundle:Orders')->find($id);
$type = $order->getPerson()->getPersonType() === 1 ? "natural" : "legal";
$orderForm = $this->createForm(new OrdersType(array($type)), $order, array(
'action' => $this->generateUrl('update-order', array('id' => $id)),
'method' => 'POST',
));
return array(
'entity' => $order,
"form" => $orderForm->createView(),
'id' => $id
);
}
然后在我看来,我按照以下方式渲染字段:
{{ form_widget(form.person.person.description) }}
代码渲染字段正确但没有值,是的,它有值,错误在哪里?这是我使用的其他表格:
LegalPersonType
class LegalPersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rif', 'text', array(
'required' => true,
'label' => false,
'attr' => array(
'maxlength' => 10,
))
)
->add('identification_type', 'choice', array(
'label' => FALSE,
'choices' => RifType::getChoices(),
'attr' => array(
'class' => 'toSelect2'
)
))
->add('person', new PersonType(), array('label' => FALSE));
}
}
处理映射
PersonType
class PersonType extends AbstractType {
....
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\Person',
'inherit_data' => true
));
}
public function getName()
{
return 'person';
}
}
NaturalPersonType
class NaturalPersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('identification_type', 'choice', array(
'label' => 'Número de Cédula',
'choices' => CIType::getChoices()
))
->add('ci', 'number', array(
'required' => true,
'label' => false,
'attr' => array(
'maxlength' => 8,
))
)
->add('person', new PersonType(), array(
'label' => FALSE,
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
public function getName()
{
return 'natural_person';
}
}
如果我从setDefaultOptions
删除了NaturalPersonType
方法,则会出现此错误:
表单的视图数据应该是标量,数组或类型 \ ArrayAccess的实例,但是是类的实例 塔南\ FrontendBundle \实体\ NaturalPerson。您可以避免此错误 通过设置" data_class"选项 "塔南\ FrontendBundle \实体\ NaturalPerson"或者通过添加视图 转换类实例的转换器 Tanane \ FrontendBundle \ Entity \ NaturalPerson to scalar,array or an \ ArrayAccess的实例。
如果我离开,我会得到另一个:
方法"描述" for object" Symfony \ Component \ Form \ FormView"不 不存在于 /var/www/html/tanane/src/Tanane/BackendBundle/Resources/views/Order/edit.html.twig 在第134行
答案 0 :(得分:2)
看起来PersonType
未正确映射到您的实体。
由于LegalPersonType
和NaturalPersonType
同时使用它来处理它们的某些属性,我会将其定义为这两个属性中的parent
。这可以通过使用inherit_data
选项(documentation)来实现。
您的代码看起来像这样:
<强> PersonType 强>
class PersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description', 'text', array(
'required' => TRUE,
'label' => FALSE
))
->add('contact_person', 'text', array(
'required' => FALSE,
'label' => 'Persona de Contacto'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'inherit_data' => true,
));
}
}
请注意,您应该从data_class
删除setDefaultOptions()
选项。
<强> LegalPersonType 强>
class LegalPersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rif', 'text', array(
'required' => true,
'label' => false,
'attr' => array(
'maxlength' => 10,
))
)
->add('identification_type', 'choice', array(
'label' => FALSE,
'choices' => RifType::getChoices(),
'attr' => array(
'class' => 'toSelect2'
)
))
->add('data', new PersonType(), array(
'label' => FALSE,
'data_class' => 'YourBundle\Entity\LegalPerson',
));
}
}
您的NaturalPersonType
看起来与LegalPersonType
类似。
现在,您可以在视图中执行此操作:
{{ form_widget(form.person.data.description) }}