Reputation: 6062
I'm not quite sure if my title is informative enough. If anyone with editing privileges wants to tweak it after reading this, feel free.
I need to create a form type that can mirror my current parent<->child, 1-to-1 relationship I have with my entities. Right now, judging from the docs, the only way I can specify the child entity is to use the Entity
form type, but that doesn't seem to give me what I want. I don't want a selection of existing entities I can add to the parent. I need to be able to fill in the fields of the parent, and then optionally fill in the fields of the child, all in the same form. Ideally, I'd be able to do something like:
$builder->add('child', 'entity', array(
'required' => false,
'class' => 'MyBundle\Entity\Child',
'type' => 'MyBundle\Form\Type\ChildType'
)
);
But, from what I can see, the type
option only exists with collections, which, again, is not what I'm dealing with.
Any suggestions?
Upvotes: 1
Views: 2825
Reputation: 11822
I'm not sure if i get what you mean exactly but this should work:
$builder->add('child', 'child_type');
http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
UPDATE
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('city','text');
$builder->add('country','country');
}
public function getName()
{
return 'my_address_form';
}
}
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name','text');
$builder->add('address','my_address_form');
//Or if you don't want to defined the child form as a service you can use
//$builder->add('address', new AddressType());
}
public function getName()
{
return 'my_user_form';
}
}
in case if you need to define AddressType
as a service:
services:
acme_demo.form.type.address:
class: Acme\DemoBundle\Form\Type\AddressType
tags:
- { name: form.type, alias: my_address_type }
Upvotes: 1