简体   繁体   中英

Symfony2: Add form in to another form

Is pretty simple:

I have a entity Agency with

/**
 * @var \Rewards\LocationBundle\Entity\Address
 *
 * @ORM\ManyToOne(targetEntity="\Acme\xxBundle\Entity\Address", cascade={"persist"})
 * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
 */
protected $address;

and I have a AgencyType for create Form:

   $builder
        ->add('name')
        ->add('address');

I have also AddressType

   $builder
        ->add('street')
        ->add('zipCode')
        ->add('city')
        ->add('country');

With this configuration if I put:

{{ form_widget(form) }} 

in the twig template I only view the 'Select' form widget but I want to see all the fields from the form AgencyType AddressType.

How I can do that?

I have solved the problem!

$builder
    ->add('name')
    ->add('agents')
    ->add('address', new AddressType());

Clean and easy!

In symfony 3/4 you should add a data class (Entity) of you entity in the AdressType form. for instance:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Address::class,
    ]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM