I'm using two form types for one page in my Symfony project. I do this to let the user decide between two options of creating a new document
How the page is build: there are several text fields, to be filled out. They all belong to my DocumentCreateType and also include the right part of the choice (select IATA(s) manually) you can see in the picture. My second form type (UploadProfileType) contains the same three dropdowns plus an addtional one (markets, channel1 and products) but on the left site of the choice (use upload profile(s)).
So depending on what the user has chosen, only the DocumentCreateType has to be submitted, or both form types have to be submitted and persisted.
How can I make this working in my Controller? So far my Controller looks like that but it's not correctly persisting the data
$upForm = $this->createForm(UploadProfileType::class, $document, array('user' => $currentuser));
$form = $this->createForm(DocumentCreateType::class, $document);
$form->handleRequest($request);
$upForm->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
...
}
The ChoiceType for the choice between upload profile and IATA looks like that and is handled by javascript:
$builder
->add('use_upload_profile', ChoiceType::class, array(
'choices' => array(
true => 'label.use_upload_profile',
false => 'label.select_iatas_manually'
),
'mapped' => false,
'expanded' => true,
'label' => false,
'data' => true,
'translation_domain' => 'Documents'))
;
}
You cannot submit two forms at the same time, it's either one or the other. So I would suggest you create two different forms:
The first one will be submitted when "Use upload profile(s)" is selected
The second one will be submitted when "Select IATA(s) manually" is selected
In each of the forms you need all the data to be submitted. If you want to avoid duplicated code in the FormType, you can create a Custom Form Type (not associated to an entity):
To handle both forms in the controller:
Then, with JavaScript, depending on which radio button is selected, you display either the first form (with its own submit button) or the second (also with its own submit button).
Multiple forms
In HTTP you can only have one submitted form on a request.
Create an endpoint for each form submit where you only submit one form.
Create Document A -> createADocumentAction()
Create Document B -> createBDocumentAction()
One form with everything
If you are calling form to a set of data but everything gets submited with a single request you should create a Symfony From (Type) that has all the data of the two symfony forms that you plan to submit.