Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

how to split long symfony form in multiple pages?

I want to create a form for an entity which has a many attributes. To ensure the ease of data entry, I want to split that form in multiple pages (for example 2 or 3 pages). Let's take the ad entity example:

  1. In page 1, the user will enter the ad text
  2. In page 2, the user will enter his contact
  3. In page 3, the user will provide the (X,Y) position of the ad

This split will require saving the available data (inserting in the database) in the 1st page before moving to the next page. Unfortunately, This is not possible due to constraints.

The question is: Is any documentations or any examples that solve this issue?

If no documentation is available, do you think it is better to split my entity in n entities in order to have one entity per page?

Thanks for you help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't have to split your entity, but your form : create 3 forms, each containing the property needed from the ad entity.

You'll need to :

  • persist (and not flush) the $ad object at every step inside your controller
  • pass the $ad object as an argument when forwarding inside your controller
  • flush the $ad object in the last step

In pseudo-code, your controller would 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...