在表单上呈现小部件后,表单值为空(不是值) [英] Form values is empty (not value) after render the widget on a form
问题描述
我有这个表格:
订单类型
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));
}
}
}
人物类型
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'
));
}
}
这是我在控制器中所做的:
This is what I'm doing in the controller:
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
);
}
然后在我看来,我渲染的字段如下:
Then in my view I'm rendering fields as follow:
{{ form_widget(form.person.person.description) }}
代码正确呈现字段但没有值,是的,它有值,错误在哪里?这是我正在使用的其他表单:
The code renders the field right but without values and yes, it has values, where is the error? This are the others Forms I'm using:
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));
}
}
制作地图
人物类型
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';
}
}
如果我从 NaturalPersonType
中删除 setDefaultOptions
方法,我会收到此错误:
If I remove the setDefaultOptions
method from NaturalPersonType
I get this error:
表单的视图数据应该是标量、数组或\ArrayAccess 的实例,但是是类的实例Tanane\FrontendBundle\Entity\NaturalPerson.你可以避免这个错误通过将data_class"选项设置为Tanane\FrontendBundle\Entity\NaturalPerson"或通过添加视图转换类实例的转换器Tanane\FrontendBundle\Entity\NaturalPerson 到标量、数组或\ArrayAccess 的实例.
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Tanane\FrontendBundle\Entity\NaturalPerson. You can avoid this error by setting the "data_class" option to "Tanane\FrontendBundle\Entity\NaturalPerson" or by adding a view transformer that transforms an instance of class Tanane\FrontendBundle\Entity\NaturalPerson to scalar, array or an instance of \ArrayAccess.
如果我照原样离开,我会得到另一个:
If I leave as it's I get this other:
对象Symfony\Component\Form\FormView"的方法描述"不存在于/var/www/html/tanane/src/Tanane/BackendBundle/Resources/views/Order/edit.html.twig在第 134 行
Method "description" for object "Symfony\Component\Form\FormView" does not exist in /var/www/html/tanane/src/Tanane/BackendBundle/Resources/views/Order/edit.html.twig at line 134
推荐答案
PersonType
似乎没有正确映射到您的实体.
It looks like PersonType
is not being mapped correctly to your entity.
因为 LegalPersonType
和 NaturalPersonType
都使用它来处理它们的一些属性,所以我将它定义为这两者的 parent
.这可以通过使用 inherit_data
选项(documentation).
Since it's being used both by LegalPersonType
and NaturalPersonType
to handle some of their properties, I would define it as parent
of those two. This can be achieved by using the inherit_data
option (documentation).
您的代码如下所示:
人物类型
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,
));
}
}
请注意,您应该从 setDefaultOptions()
中删除 data_class
选项.
Note that you should remove the data_class
option from 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
.
Your NaturalPersonType
would look similar to LegalPersonType
.
现在,您可以在您的视图中执行此操作:
Now, you can do this in your view:
{{ form_widget(form.person.data.description) }}
这篇关于在表单上呈现小部件后,表单值为空(不是值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!