craue / CraueFormFlowBundle

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

When redirect after submit is enabled, back and reset buttons don't update URL parameters? #412

Open gremo opened 1 year ago

gremo commented 1 year ago

Here is my controller. I'm trying to make the "redirect after submit" option work:

public function showCounselingFlow(CounselingRequestFlow $formFlow, FormFlowUtil $util, Request $request)
{
    $model = new CounselingRequest();

    $formFlow->bind($model);
    $form = $formFlow->createForm();
    if ($formFlow->isValid($form)) {
        $formFlow->saveCurrentStepData($form);

        if (!$formFlow->nextStep()) {
            $formFlow->reset();

            return $this->redirectToRoute('_complete');
        }

        if ($formFlow->redirectAfterSubmit($form)) {
            return $this->redirectToRoute(
                $request->attributes->get('_route'),
                $util->addRouteParameters($request->attributes->get('_route_params'), $formFlow)
            );
        }

        $form = $formFlow->createForm();
    }

    return [
        'form' => $form->createView(),
        'flow' => $formFlow,
    ];
}
  1. Start the flow, no URL parameters (OK)
  2. Press next, URL is updated (i.e. http://localhost/?instance=eo-6lVKSna&step=2)
  3. Press back, you are back at step 1 (OK) but URL isn't updated (still show step 2). The same happens pressing reset

Am I'm doing something wrong in my controller?

gremo commented 8 months ago

Did you find a solution?

Thanks to https://github.com/craue/CraueFormFlowBundle/issues/221 I think I've found a working solution:

<?php

namespace App\Controller;

use App\Form\TherapyRequestFlow;
use App\Form\TherapyRequestModel;
use Craue\FormFlowBundle\Util\FormFlowUtil;
use Symfony\Bridge\Twig\Attribute\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;

class TestController extends AbstractController
{
    #[Route('/test', name: 'app_test')]
    #[Template('test/index.html.twig')]
    public function test(TherapyRequestFlow $flow, FormFlowUtil $utils, Request $request): RedirectResponse|array
    {
        $model = new TherapyRequestModel();
        $flow->bind($model);

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

            if (!$flow->nextStep()) {
                $flow->reset();
                dd($model);
            }

            $form = $flow->createForm();
        }

        if ($flow->redirectAfterSubmit($submittedForm)) { // IMPORTANT: do not use $form
            return $this->redirectToRoute(
                $request->attributes->get('_route'),
                $utils->addRouteParameters(
                    [
                        ...$request->query->all(),
                        ...$request->attributes->get('_route_params'),
                    ],
                    $flow,
                )
            );
        }

        return [
            'form' => $form,
            'flow' => $flow,
        ];
    }
}