craue / CraueFormFlowBundle

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

Save on current step button #376

Closed mcgoode closed 3 years ago

mcgoode commented 3 years ago

Any advice on how to have a button that saves everything in the form to the DB?

I know I could probably do a flush on next, but want I wanted a separate button for save and quit?

craue commented 3 years ago

You could use DoctrineStorage instead of the (default) SessionStorage.

mcgoode commented 3 years ago

I opted to just persist then redirect.

I added an HiddenType input to the step(s) i wanted to have the save and quit functionality.

...
   ->add('saveProgress',HiddenType::class,[
           'mapped'=>false,
    ])
 ...

On the form I added a button to attach a JS event to:

<button type="button" data-intro="To save your progress and come back later" class=" submit-flow btn btn-success save_and_quit float-right ml-2">Save and quit</button>

Simple JS

        $('button.save_and_quit').on('click',function (){
            $('#{{ form.vars.id }}_saveProgress').val(true)
            $('form[name={{ form.vars.id }}]').submit();
        })

In the controller:

            // if we find that save progress is set to true we save everything and redirect to is order list.
            if( $request->get($form->getName())['saveProgress'] == true){
                $this->addFlash('success','Order successfully saved!');

                dump($order);
                // flow finished
                $em = $this->getDoctrine()->getManager();
                $em->persist($order);
                $em->flush();
                $flow->reset(); // remove step data from the session

                return $this->redirect($this->generateUrl('orders')); // redirect when done
            }
mcgoode commented 3 years ago

Sorry for the open an close not sure what happen.