How to split symfony long form into multiple pages?

I want to create a form for an object with many attributes. To provide easy data entry, I want to split this form into several pages (for example, 2 or 3 pages). Take an example ad:

  • On page 1, the user enters the text of the ad
  • On page 2, the user enters his contact
  • On page 3, the user will provide the position (X, Y) of the ad

This split will require saving the available data (inserting into the database) on the first page before moving on to the next page. Unfortunately, this is not possible due to limitations.

The question arises: are there any documents or any examples that solve this problem?

If the documentation is not available, do you think it is better to divide my entity into n entities in order to have one entity per page?

Thanks for the help.

+6
source share
3 answers

You should probably use CraueFormFlowBundle . It provides the ability to create multi-stage forms.

You can create one form type for the entire stream or one form type for each step.

It is very easy to set up. Everything is explained here .

+10
source

You do not need to separate the entity, but your form: create 3 forms, each of which contains the property necessary for the ad object.

You will need:

  • save (not clean) the $ ad object at every step inside your controller.
  • pass the $ ad object as an argument when transferring inside your controller
  • run the $ ad object in the last step

In pseudo code, your controller will look like this:

public function newAdStep1() { new Ad() // New instance of $ad new formStep1($ad) // The first form containing only the ad text field // The form was filled, manage it... form->isValid()? { persist($ad); // Persist the first part of your ad object forward(newAdStep2, $ad) // Go on to step 2, your $ad object as an argument } // ... or display step1 to user createView createAdStep1.html.twig('form' => $form); } public function newAdStep2($ad) { new formStep2($ad); // Now the second form, containing the "contact" fields isValid ? { persist($ad) forward(newAdStep3, $ad) } createView createAdStep2($form, $ad); // Your $ad object needs to be sent to the view } public function newAdStep3($ad) { new formStep3($ad); // Third and last form, containing the (X,Y) fields isValid ? { $em->persist($ad); $em->flush(); // Your instance of $ad can be stored in database now return('success !'); } return view createAdStep3($form, $ad); } 
+7
source

You can save all the data presented in a session or temporary table, and then save everything together at the end. However, I am working hard to avoid extra work like this.

I think it is entirely possible that your form steps follow the order in which the constraints are defined.

Speaking about the fact that sometimes I think that such a problem can be solved by making a more effective decision on the design or process. those. limit the number of questions or just ask first about vital issues. Not knowing how to do this is hard to understand.

0
source

All Articles