'Symfony single FormType to input data for several Entity attributes?

Let's say I have two distinct entities named DanceTeacher and DanceSchool:

class DanceTeacher
{
    $firstName;
    $lastName;
    $email;
    $phone;
    $street;
    $city;
}

class DanceSchool
{
    $name;
    $emailContact;
    $phoneContact;
    $street;
    $city;
}

I would like to build a FormType regrouping the 4 similar fields:

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class) // serves both teacher email and school emailContact
            ->add('Phone', TextType::class)  // serves both teacher phone and school phoneContact
            ->add('Street', TextType::class) // serves both teacher and school street
            ->add('City', TextType::class)   // serves both teacher and school city
        ;
    }
}

Then, my teacher and school forms have some common fields, and I could build them like:

class DanceTeacherType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('firstName', TextType::class)
            ->add('lastName', TextType::class)
            ->add(SOMETHING_HERE, ContactType::class, SOME_OPTIONS)
            ->add('save', SubmitType::class);
    }
}

class DanceSchoolType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class)
            ->add(SOMETHING_HERE, ContactType::class, SOME_OPTIONS)
            ->add('save', SubmitType::class);
    }
}

Is that possible? I can't find any doc about having a single FormType to write into several entity attributes. I thought I could find options to define some sort of "mapping" between FormType sub-fields and my underlying entities' attributes.

Anyway, put like this, I'm not sure I would win a lot in factorizing in such a way.

Thanks in advance

(oh yes I know, "don't use entities in forms" ;-)



Solution 1:[1]

I think that what you want to do is documented here :

https://symfony.com/doc/current/form/form_collections.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zabon