Symfony - Pasing values ​​from one form to another

I read everything about Symfony and Twig forms, but could not find a solution for my problem, so I decided to ask you guys.

What I'm trying to achieve is to submit duplicate data to my registration form only once. The data that I want to save in the database is the user data, company name and address, and the name and address of the branch. I need to copy the address data into two objects, i.e. Company and branch.

Is there a way to transfer this data for creation in the web interface only once and point it to two Symfony forms that will be added to the entities and validated.

I know that I can copy address data from one object to another outside the form, but this does not seem to be correct.

Main RegistrationFormType:

class RegistrationFormType extends AbstractType
{
    private $class;

    public function __construct($class)
    {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('first_name')
            ->add('last_name')
            ->add('username')
            ->add('email', 'email')
            ->add('password', 'password')
            ->add('company', new CompanyType())
            ->add('branch', new BranchType())
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    public function getName()
    {
        return 'registration';
    }
}

CompanyType:

class CompanyType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('companyName')
            ->add('foo', new CompanyInfoType(), array('data_class' => 'Acme\UserBundle\Entity\Company'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\UserBundle\Entity\Company',
            'validation_groups' => array('Registration'),
        ));
    }

    public function getName()
    {
        return 'company';
    }
}

Branch Type:

class BranchType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('branchName')
            ->add('boo', new CompanyInfoType(), array('data_class' => 'Acme\UserBundle\Entity\Branch'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\UserBundle\Entity\Branch',
            'validation_groups' => array('Registration'),
        ));
    }

    public function getName()
    {
        return 'branch';
    }
}

CompanyInfoType:

class CompanyInfoType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('streetNumber')
            ->add('address')
            ->add('city')
            ->add('zip')
            ->add('country')
            ->add('contactName')
            ->add('phone')
            ->add('email')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'inherit_data' => true
        ));
    }

    public function getName()
    {
        return 'company_info';
    }
}

!

//EDIT

, , .

, , ​​ , "". , - , , , "". , - .

//EDIT2 linuxatico, , . :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('first_name')
        ->add('last_name')
        ->add('username')
        ->add('email', 'email')
        ->add('password', 'password')
        ->add('company', new CompanyType())
        ->add('branch', new BranchType())
    ;

    $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
        $company = $event->getForm()->get('company')->getData();
        $event->getForm()->get('branch')->getData()
            ->setBranchName($company->getCompanyName())
            ->setStreetNumber($company->getStreetNumber())
            ->setAddress($company->getAddress())
            ->setCity($company->getCity())
            ->setCountry($company->getCountry())
            ->setZip($company->getZip())
            ->setContactName($company->getContactName())
        ;
    });
}
+4
1

, , , , , , , , , , , .

, , AJAX, JQuery, POST , .

:

$.post({ url1,
         key_value_array_data,
         function(){
             $.post({ url2,
                      key_value_array_data,
                      function(){
                           //custom_code
                    });
        }
}); 

EDIT: : https://api.jquery.com/serialize/

EDIT # 2: Symfony, : http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

+2

Source: https://habr.com/ru/post/1538964/


All Articles