在窗体上呈现小部件后,窗体值为空(不是值)


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'
        ));
    }
}

这就是我在控制器中所做的:

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));
    }
}

正在进行映射

人物类型

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方法,我会得到以下错误:

表单的视图数据应为标量、数组或''ArrayAccess的实例,但它是类的实例Tanane''FrontendBundle''Entity''NaturalPerson。您可以避免此错误通过将"data_class"选项设置为"Tanane''FrontendBundle''Entity''NaturalPerson"或通过添加视图转换类实例的转换器Tanane''FrontendBundle''Entity''NaturalPerson转换为标量、数组或''ArrayAccess的实例。

如果我照原样离开,我会得到另一个:

对象"Symfony''Component''Form''FormView"的方法"description"不存在于/var/www/html/tanane/src/tanane/BackendBundle/Resources/views/Order/edit.html.twig在第134行

看起来PersonType没有正确映射到您的实体。

由于LegalPersonTypeNaturalPersonType都在使用它来处理它们的一些属性,所以我将其定义为这两个属性中的parent。这可以通过使用inherit_data选项(文档)来实现。

你的代码看起来像这样:

人物类型

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选项。

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) }}