How to combine and process 2 symfony forms in one?

I have 2 forms of symfony:

SignupFormType and HouseRentFormType

The registration form is as follows:

 <form ..> <input name='email' .. /> <input name='pass' .. /> .. </form> 

And the rent of the House is formed as follows:

 <form ..> <input name='city' .. /> <input name='price' .. /> </form ..> 

I want to combine them so that they look like this:

 <form ..> // house rent info: <input name='city' .. /> <input name='price' .. /> //registration info: <input name='email' .. /> <input name='pass' .. /> <input type='submit' /> </form ..> 

And also create a form type or smthng. Any tips on how to handle submit?

ps I am using symfony / form: ^ 3.0

+3
source share
2 answers

With Symfony forms, this is all a type of form. Thus, one of them has a root type with child types. Each child type can have other child types, etc.

So, in this case, you have 2 types of forms: SignupFormType and HouseRentFormType . You can use them as child types of the new form:

 $form = $formBuilder ->add('signup', SignupFormType::class) ->add('house_rent', HouseRentFormType::class) ->getForm(); 
+7
source

I would like to expand @Wouter J's answer because it did not work for me, as above.

I needed to define the data_class class in formbuilder:

 class SignupHouseRentFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('signup', SignupFormType::class, array( 'data_class' => Signup::class )) ->add('house_rent', HouseRentFormType::class, array( 'data_class' => HouseRent::class )); } } 

then in the controller I had to use the same namespace for data binding:

 class SignupHouseRentController extends Controller { public function indexAction(Request $request) { $signup = new Signup(); $houseRent = new HouseRent(); $mergedData = array( 'signup' => $signup, 'houseRent' => $houseRent ); $form = $this->createForm(SignupHouseRentFormType::class, $mergedData); $form->handleRequest($request); } } 
+3
source

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


All Articles