how to split long symfony form in multiple pages?

后端 未结 3 1239
南旧
南旧 2020-12-15 09:47

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).

相关标签:
3条回答
  • 2020-12-15 10:14

    You probably should use CraueFormFlowBundle. It provides facilities for building multi-step forms.

    You can create one form type for an entire flow, or one form type per step.

    It's very easy to setup. Everything is explained here.

    0 讨论(0)
  • 2020-12-15 10:23

    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);
    }
    
    0 讨论(0)
  • 2020-12-15 10:36

    You could store all the submitted data in a session or temporary table, then persist it all together at the end. However, I do my best to avoid extra work like this.

    I think it may well be that your form steps follow the order by which the constraints dictate.

    Saying that, sometimes I think this kind of problem can be solved by making a better design or process decision. i.e. limit the number of questions or only ask the vital ones at first. Without knowing the ins and outs it's hard to know whether this can be done.

    0 讨论(0)
提交回复
热议问题