-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Add FormFlow
for multistep forms management
#60212
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
base: 7.4
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
early raw reviews :)
src/Symfony/Component/Form/Extension/Core/Type/FormFlowNavigatorType.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Extension/Core/Type/FormFlowType.php
Outdated
Show resolved
Hide resolved
...ony/Component/Form/Extension/HttpFoundation/Type/FormFlowTypeSessionDataStorageExtension.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Extension/Core/Type/FormFlowActionType.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Extension/Core/Type/FormFlowNavigatorType.php
Outdated
Show resolved
Hide resolved
Great idea! I've looked at the previous PR and could use it! One question came up: how can I jump back to a specific step without having to press the back button many times? |
Hey! take a look at the |
2f81d34
to
cdac9e2
Compare
Hey @yceruto, awesome to see this may become a native part of Symfony Forms! I just had a quick look at the implementation and didn't find anything about It would be very helpful to have a default way the FormFlow can handle file uploads, but also to have some interface to interact with the full lifecycle of uploaded files, e.g. to decide where and how to store them, how to reference them inside form data, etc. I think the lifecycle consists of the following steps:
Maybe file uploads are not as important for an initial implementation, but it's definitely a use case which should be thought about. Feel free to ping me if you have any questions. Maybe I could even build a minimal demo of the current implementation we use in our project and share it with you. |
this would be incompatible with apps using a load balancer with several servers, as there is no guarantee that the next request goes to the same server behind the load balancer. Such case require storing uploads in a shared storage (for instance using a S3 bucket or similar storage) |
You're very right, thanks for pointing this out. So it's especially important to have the possibility of handling those different cases. Maybe a |
I haven’t checked the file upload yet because I knew it would be a complicated topic, but I think using a custom flow button (as explained above) will give you the flexibility to do custom things on your own. One option is to create your own Imagine you have a $builder->add('upload', FlowButtonType::class, [
'handler' => function (MyDto $data, FlowButtonInterface $button, FormFlowInterface $flow) {
// handle files uploading here ... store them somewhere ... create references ...
// $data->uploadedFiles = ... save references if we go back ...
$flow->moveNext();
},
'include_if' => ['documents'], // the steps where this button will appear
]); So, it’s up to you where to store the files, how to reference them in the DTO, and how to render them again if the user goes back to this step, just by looking into |
Think of Flow buttons like mini-controllers with a focused job. The main controller handles the shared logic across all steps, while each action handler takes care of custom operations. You can even use them for inter-step operations (like in the demo preview, where I showed how to add or remove skill items from a This part feels a bit like magic, but it’s a helpful one given how complex multistep forms can be. |
1ef730f
to
4df48d8
Compare
As promised, here’s the demo app: https://github.com/yceruto/formflow-demo. I’d love to see what other use cases you think this feature could support, feel free to share your ideas! |
Hi @yceruto, Thank you very much for the demo. I found it really straightforward to understand how to use and customize the form, the steps, and the actions 👍🏽 . I’ve tested several use cases, and everything works well except for one scenario. I might be missing something, but it seems that the form events on the steps are not being triggered. Specifically, in a flow where the user is first asked to pick a color (choice type), and then to select a gradient based on the chosen color, the gradient step uses a choice type with options depending on the previous selection. Unfortunately, the event responsible for setting these dynamic choices is never called. I might be misunderstanding how to properly use form events with the form flow, but I wanted to check with you in case there’s a recommended way to handle this scenario. Other than this issue, everything else is working perfectly. |
Hi @Spomky, thanks for testing it :) Can you please share the changes that I need to reproduce the issue? Thanks! |
Hi, As mentioned in the PR I created to show the issue, I eventually managed to get it working. So to me, this is definitely a very good addition, allowing lots of use cases to be covered. Thank you very much for your work on this! I really appreciate the time and effort you put into it. Approved! This could be marked as experimental for 7.4 and officially adopted in 8.0. |
Unfortunately we cannot introduce experimental features in LTS versions. See https://symfony.com/doc/current/contributing/code/experimental.html |
Indeed. So let's play with it and we'll see starting from 8.1. |
Would it be possible to already provide it via an experimental "addon" bundle (e.g. symfony/experimental-form-flow) before it's released with the official Symfony Forms component? That way it could already be battle-tested with a current stable release (7.3+ or even earlier). Waiting until 8.1 (released in May 2026) for a first experimental release seems like a pretty long time to me. |
If there's a need to mark it as experimental, it could be added as part of 8.0 as experimental, like symfony/string was for version 5.0. |
@RafaelKr there's already a bundle port of the feature that you can use: https://github.com/yceruto/formflow-bundle The demo app use it! |
Absolutely! I'm currently beta testing this feature in two projects using yceruto/formflow-bundle and everything is working good so far. However, It would be fantastic to get feedback from some of the more experienced Form contributors, many come to mind, but I'd love to hear the thoughts of @xabbuh, @weaverryan, @wouterj, @HeahDude and @stof in particular if possible 🙏 Naming conventions, design, DX, etc. there is always room for improvement. |
380da70
to
9ba68de
Compare
I received feedback regarding the flow button handler logic, so I simplified it by delegating execution responsibilities directly to the In general, you don’t need to manually call However, you might occasionally need to invoke the handler associated with the clicked flow button manually, such as when you want to perform additional logic after its execution but before the new step form: $flow->handleRequest($request);
$flow->getClickedButton()->handle();
// do something between the current step and the new step form creation...
$flow = $flow->getStepForm(); The related changes are visible here. |
9cb35db
to
ab49dc4
Compare
Thanks @smnandre for your review! Summary of the latest changes:
The description of the PR was also updated to reflect the current state of the code. It's ready for code review again! |
Alternative to
MultiStepType
#59548Inspired on @silasjoisten's work and @craue's CraueFormFlowBundle, thank you!
FormFlow
This PR introduces
FormFlow
, a kind of super component built on top of the existingForm
architecture. It handles the definition, creation, and handling of multistep forms, including data management, submit buttons, and validations across steps.Demo app: https://github.com/yceruto/formflow-demo
AbstractFlowType
Just like
AbstractType
defines a single form based on theFormType
,AbstractFlowType
can be used to define a multistep form based onFormFlowType
.The step name comes from the first param of
addStep()
, which matches the form name, like this:personal
form of typeUserSignUpPersonalType
will be the steppersonal
,professional
form of typeUserSignUpProfessionalType
will be the stepprofessional
,When the form is created, the
currentStep
value determines which step form to build, only the matching one, from the steps defined above, will be built.Controller
Use the existent
createForm()
in your controller to create aFormFlow
instance.This follows the classic form creation and handling pattern, with 2 key differences:
$flow->isFinished()
to know if the finish flow button was clicked,$flow->getStepForm()
call, which creates the a new step form, when necessary, based on the current state.Don’t be misled by the
$flow
variable name, it’s just aForm
descendant withFormFlow
capabilities.Important
The form data will be stored across steps, meaning the initial data set during the FormFlow creation won't match the one returned by
$form->getData()
at the end. Therefore, always use$form->getData()
when the flow finishes.FlowButtonType
A FlowButton is a regular submit button with a handler (a callable). It mainly handles step transitions but can also run custom logic tied to your form data.
There are 4 built-in FlowButton types:
FlowResetType
: sends the FormFlow back to the initial state (will depend on the initial data),FlowNextType
: moves to the next step,FlowPreviousType
: goes to a previous step,FlowFinishType
: same asreset
but also marks the FormFlow as finished.You can combine these options of these buttons for different purposes, for example:
skip
button using theFlowNextType
andclear_submission = true
moves the FormFlow forward while clearing the current step,back_to
button using theFlowPreviousType
and a view value (step name) returns to a specific previous step,Built-in flow buttons will have a default handler, but you can define a custom handler for specific needs. The
handler
option uses the following signature:Important
By default, the callable handler is executed when the form is submitted, passes validation, and just before the next step form is created during
$flow->getStepForm()
. To control it manually, check if$flow->getClickedButton()
is set and call$flow->getClickedButton()->handle()
after$flow->handleRequest($request)
where needed.FlowButtonType
also comes with other 2 options:clear_submission
: If true, it clears the submitted data. This is especially handy forskip
andprevious
buttons, or anytime you want to empty the current step form submission.include_if
:null
if you want to include the button in all steps (default), an array of steps, or a callable that’s triggered during form creation to decide whether the flow button should be included in the current step form. This callable will receive theFlowCursor
instance as argument.Other Building Blocks
FlowCursor
This immutable value object holds all defined steps and the current one. You can access it via
$flow->getCursor()
or as aFormView
variable in Twig to build a nice step progress UI.FlowNavigatorType
The built-in
FlowNavigatorType
provides 3 default flow buttons:previous
,next
, andfinish
. You can customize or add more if needed. Here’s an example of adding a “skip” button to theprofessional
step we defined earlier:Then use
UserSignUpNavigatorType
instead.Data Storage
FormFlow handles state across steps, so the final data includes everything collected throughout the flow. By default, it uses
SessionDataStorage
(unless you’ve configured a custom one). For testing,InMemoryDataStorage
is also available.You can also create custom data storage by implementing
DataStorageInterface
and passing it through thedata_storage
option inFormFlowType
.Step Accessor
The
step_accessor
option lets you control how the current step is read from or written to your data. By default,PropertyPathStepAccessor
handles this using the form’s bound data andPropertyAccess
component. If the step name is managed externally (e.g., by a workflow), you can create a customStepAccessorInterface
adapter and pass it through this option inFormFlowType
.Validation
FormFlow relies on the standard validation system but introduces a useful convention: it sets the current step as an active validation group. This allows step-specific validation rules without extra setup:
Allowing you to configure the validation
groups
in your constraints, like this:Type Extension
FormFlowType is a regular form type in the Form system, so you can use
AbstractTypeExtension
to extend one or more of them:There’s a lot more to share about this feature, so feel free to ask if anything isn’t clear.
Cheers!