craue / CraueFormFlowBundle

Multi-step forms for your Symfony project.
MIT License
736 stars 118 forks source link

Use of CollectionType ; data disappears when going back #282

Closed jbennegent closed 7 years ago

jbennegent commented 7 years ago

Hello,

First, thanks for your work on this bundle. It's really usefull to me.

I have some questions about my use of it. I'm not sure if what I'm looking to do it possible or not.

My form is about a User, in 3 steps. There are 3 Contacts (entities) linked to a User.

Step 1 : Basic info of User entity Step 2 : Informations about the 3 Contacts (linked entities) Step 3 : unmapped field

When I do 1 => 2 => 3 => Submit : works well When I do 1 => 2 => 3 => 2, contacts are well displayed, with data entered before.

But when doing 1 => 2 => 3 => 2 => 1 => 2, the contacts data disappear. Note the User data of step 1 are here.

Here is a piece of my controller.

public function myControllerAction(Request $request)
    {
        $newUser = new MyUser(); 

        if ($newUser->getContacts()->count() == 0)
        {
            for ($i=0 ; $i<3 ; $i++)
            {
                $newUser->getContacts()->add(new Contact());
            }
        }

        $flow = $this->get('my_project.form.flow.my_flow');
        $flow->bind($newUser);

        $form = $flow->createForm();        

        if ($flow->isValid($form)) {
            $flow->saveCurrentStepData($form);

            if ($flow->nextStep()) {
                // form for the next step
                $form = $flow->createForm();
            } else {
                // flow finished
                // We need to get data this way as it is not mapped, hence not persisted anywhere else

Is the way I deal with Contacts OK ? The strange thing, in my opinion, is that when I get back from step 3 to 2, Contacts are there, but not if I do 1 => 2 => 3 => 2 => 1 => 2 (as told earlier).

Thanks

craue commented 7 years ago

When going back to step 1, the data for all subsequent steps (> 1) is deleted. This is actually the expected behavior because the next time step 2 is used, its form could be built completely different based on the (changed) data given in step 1. To avoid that, you could enable dynamic step navigation (check the readme).

jbennegent commented 7 years ago

Wow, this is that simple. Thanks a lot.