【问题标题】:Variable has value but form display empty, why?变量有值但表单显示为空,为什么?
【发布时间】:2014-09-14 16:59:44
【问题描述】:

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

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 模板中,我对 form var 进行了调试,我得到了这个:

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

  1. 使用来自entity 我传递给视图的所有信息找到解决方案并显示正确的表单。这是理想状态,我愿意为此努力学习,请帮忙?
  2. 在控制器中获取Person 对象并通过传递Person 对象值创建第二个表单,这应该可以工作,但是我需要对update 函数进行大量更改,因为表单将分开传输。李>

我需要在任何时候编辑现有的Orders 时将NaturalPersonTypeLegalPersonType 嵌入OrdersType,因为现在我不知道如何在树枝模板中显示小部件。注意最后我在这种情况下谈论的表单是 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 的代码会很有用。
  • @FyodorX 我添加了一些额外的信息看看,如果您需要其他信息,请告诉我

标签: php forms symfony symfony-forms


【解决方案1】:

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

我猜你这样做是因为你的模型中没有这些属性,但你确实person。您需要做的是将NaturalPersonTypeLegalPersonType 映射到person

在您的情况下,最简单的方法是将OrdersTypebuildForm() 的最后几行替换为:

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

【讨论】:

  • 如果我这样做,我会得到这个错误:Neither the property "nat" nor one of the methods "getNat()", "nat()", "isNat()", "hasNat()", "__get()" exist and have public access in class "Tanane\FrontendBundle\Entity\Orders".。要获得我的实体映射的完整视图,请参阅 this post,因为我使用的是 Doctrine 类表继承,那么我如何让它工作?
  • 你确定这两个字段都叫person吗? nat 不应该再出现了。
  • 在这种情况下是的,因为这是从 Fixtures 加载的测试数据,是的,所有这些数据都属于 NaturalPersonType,你想到什么来解决这个问题?
  • 你能发帖NaturalPersonType吗?
  • 是的,已添加到主帖,看看
猜你喜欢
  • 1970-01-01
  • 2014-04-14
  • 2013-08-03
  • 1970-01-01
  • 2017-04-18
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
相关资源
最近更新 更多