dpwfh874876 2014-09-14 16:59
浏览 28
已采纳

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

I'm editing a object using this code:

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

All there works fine except that I'm not know/find how to display object Person values. If you take a look at the picture I've attached here you'll notice that Person comes with values:

Controller Debug

In the other side I did the same but at Twig template and I make a debug to the form var and I get this:

Twig Render

Now at this point I'm confused and come with two possible ideas, that I hope, someone help me to develop or at least understand.

  1. Find a solution and display the right form using all the info from the entity I'm passing to the view. This is the ideal and I'll like to work on this in order to learn, so help?
  2. Get the Person object in controller and create a second form by passing Person object values, this should work but them I'll need a lot of changes in update function since forms will travel separately.

What I need here is to get the NaturalPersonType or LegalPersonType embedded in OrdersType any time I edit a existent Orders because right now I don't know how to display the widget in the twig template. Notice at the end the form I'm talking about in this case is a NaturalPersonType rendered but without values:

enter image description here

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

}

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

}
  • 写回答

1条回答 默认 最新

  • doucheng7808 2014-09-14 17:42
    关注

    The problem is in your Type. As you can see, the mapped option is set to false for both nat and leg. This means that Symfony will not connect those fields with your entity, so they are empty when you render the view.

    I guess you did it that way because you don't have either of those properties in your model, but you do have person. What you need to do is to map NaturalPersonType or LegalPersonType to person.

    The easiest way to do that in your case would be to replace the last lines of your OrdersType's buildForm() with this:

        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));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退