【问题标题】:Form values is empty (not value) after render the widget on a form在表单上呈现小部件后,表单值为空(不是值)
【发布时间】:2014-09-14 20:03:14
【问题描述】:

我有这个表格:

订单类型

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”的方法“描述”确实 不存在于 /var/www/html/tanane/src/Tanane/BackendBundle/Resources/views/Order/edit.html.twig 在第 134 行

【问题讨论】:

    标签: php symfony symfony-forms


    【解决方案1】:

    PersonType 似乎没有正确映射到您的实体。

    由于LegalPersonTypeNaturalPersonType 都使用它来处理它们的一些属性,我将其定义为这两者中的parent。这可以通过使用 inherit_data 选项 (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 选项。

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

    【讨论】:

    • 我把测试结果加到主帖里了,看看还是不行
    • PersonType 中删除'data_class' => 'Tanane\FrontendBundle\Entity\Person',。将setDefaultOptions 留在NaturalPersonType。现在,Order/edit.html.twig 的第 134 行是什么?
    • 好的,我照你说的做,但在第 134 行我有这个{{ form_widget(form.person.description) }},我需要清除缓存吗?
    • 我已经更新了我的答案。基本上,在你的NaturalPersonType 中为这个->add('data', new PersonType(), array( 更改->add('person', new PersonType(), array(。现在,在你看来,这样做{{ form_widget(form.person.data.description) }}
    • 是的,我已经通过尝试{{ form_widget(form.person.person.description) }} 得到了它,它有效,谢谢,让我看看这是否不会破坏我的其他东西
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2014-12-23
    • 1970-01-01
    相关资源
    最近更新 更多