繁体   English   中英

变量有值,但表格显示为空,为什么?

[英]Variable has value but form display empty, why?

我正在使用以下代码编辑对象:

public function editAction($id = null)
{
    $em = $this->getDoctrine()->getManager();
    $order = $em->getRepository('FrontendBundle:Orders')->find($id);
    $type = $order->getPerson()->getPersonType() === 1 ? "natural" : "legal";

    ladybug_dump_die($order);

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

除了不知道/找不到如何显示对象Person值之外,所有其他方法都工作正常。 如果您看一下我在此处附加的图片,您会发现Person带有值:

控制器调试

另一方面,我做了同样的事情,但是在Twig模板上,我对var form进行了调试,我得到了:

树枝渲染

现在,我感到困惑,并提出了两个可能的想法,希望有人能帮助我发展或至少理解。

  1. 查找解决方案,并使用我要传递给视图的entity所有信息来显示正确的表单。 这是理想的选择,我想继续学习以便学习,有帮助吗?
  2. 在控制器中获取Person对象,并通过传递Person对象值来创建第二个表单,这应该可以工作,但是由于表单将单独传播,因此我需要在update函数中进行很多更改。

我需要在这里得到NaturalPersonTypeLegalPersonType嵌入OrdersType任何时候,我编辑存在的Orders ,因为现在我不知道如何显示在树枝模板中的小部件。 最后请注意,在这种情况下,我要讨论的表单是NaturalPersonType呈现的但没有值:

在此处输入图片说明

添加OrdersType FormType

class OrdersType extends AbstractType {

    /**
     * @var string
     */
    protected $register_type;

    public function __construct($register_type)
    {
        $this->register_type = $register_type;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('nickname', 'text', array(
                    'required' => FALSE,
                    'label' => "Nickname/Seudónimo",
                    'trim' => TRUE,
                    'attr' => array(
                        'class' => 'nickname'
                    )
                ))
                ->add('email', 'email', array(
                    'required' => TRUE,
                    'label' => "Correo Electrónico",
                    'trim' => TRUE
                ))
                ->add('phone', 'tel', array(
                    'required' => TRUE,
                    'label' => 'Números de teléfono (separados por "/")',
                    'trim' => TRUE,
                    'default_region' => 'VE',
                    'format' => PhoneNumberFormat::NATIONAL
                ))
                ->add('fiscal_address', 'textarea', array(
                    'required' => TRUE,
                    'label' => 'Dirección'
                ))
                ->add('shipping_address', 'textarea', array(
                    'required' => TRUE,
                    'label' => 'Dirección de Envío'
                ))
                ->add('shipping_from', 'choice', array(
                    'label' => 'Compañía de Encomiendas',
                    'choices' => SFType::getChoices(),
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('payment_type', 'entity', array(
                    'class' => 'CommonBundle:PaymentType',
                    'property' => 'name',
                    'required' => TRUE,
                    'label' => 'Forma de Pago',
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('order_amount', 'number', array(
                    'label' => 'Monto',
                    'required' => TRUE,
                    'precision' => 2
                ))
                ->add('bank', 'entity', array(
                    'class' => 'CommonBundle:Bank',
                    'property' => 'name',
                    'required' => TRUE,
                    'label' => 'Banco',
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('transaction', 'text', array(
                    'required' => TRUE,
                    'label' => 'No. Transacción'
                ))
                ->add('comments', 'textarea', array(
                    'required' => FALSE,
                    'label' => 'Comentarios'
                ))
                ->add('secure', 'checkbox', array(
                    'label' => FALSE,
                    'required' => FALSE,
                    'value' => 1,
                    'mapped' => FALSE
                ))
                ->add('lives_in_ccs', 'checkbox', array(
                    'label' => false,
                    'required' => false,
                    'mapped' => FALSE,
                    'value' => 1,
                ))
                ->add('suscribe_mail_list', 'checkbox', array(
                    'label' => FALSE,
                    'required' => FALSE,
                    'value' => 1,
                    'mapped' => FALSE
        ));

        if ($this->register_type[0] == "natural")
        {
            $builder->add('nat', new NaturalPersonType(), array('mapped' => FALSE));
        }
        elseif ($this->register_type[0] == "legal")
        {
            $builder->add('leg', new LegalPersonType(), array('mapped' => FALSE));
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
            'render_fieldset' => FALSE,
            'show_legend' => FALSE,
            'intention' => 'orders_form'
        ));
    }

    public function getName()
    {
        return 'orders';
    }

}

添加NaturalPersonType

<?php

namespace Tanane\FrontendBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\CIType;

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('mapped' => FALSE));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
        ));
    }

    public function getName()
    {
        return 'natural_person';
    }

}

问题出在您的Type 如您所见, natlegmapped选项都设置为false 这意味着Symfony不会将这些字段与您的实体连接,因此在渲染视图时它们为空。

我猜你做到了,那是因为你没有任何这些属性在模型中,但必须person 你需要做的是映射NaturalPersonTypeLegalPersonTypeperson

在这种情况下,最简单的方法是用以下命令替换OrdersTypebuildForm()的最后OrdersType行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM