【发布时间】: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 进行了调试,我得到了这个:
现在我很困惑,并提出了两个可能的想法,我希望有人帮助我发展或至少理解。
- 使用来自
entity我传递给视图的所有信息找到解决方案并显示正确的表单。这是理想状态,我愿意为此努力学习,请帮忙? - 在控制器中获取
Person对象并通过传递Person对象值创建第二个表单,这应该可以工作,但是我需要对update函数进行大量更改,因为表单将分开传输。李>
我需要在任何时候编辑现有的Orders 时将NaturalPersonType 或LegalPersonType 嵌入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