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:
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:
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.
- 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? - Get the
Person
object in controller and create a second form by passingPerson
object values, this should work but them I'll need a lot of changes inupdate
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:
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';
}
}