Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gremo opened this issue May 24, 2023 · 1 comment

Comments

@gremo
Copy link

gremo commented May 24, 2023

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
Copy link
Author

gremo commented Mar 27, 2024

Did you find a solution?

Thanks to #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,
        ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant