Download the PHP package megawilddaddy/formflow-bundle without Composer
On this page you can find all versions of the php package megawilddaddy/formflow-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download megawilddaddy/formflow-bundle
More information about megawilddaddy/formflow-bundle
Files in megawilddaddy/formflow-bundle
Package formflow-bundle
Short Description Multi-step forms for your Symfony project.
License MIT
Homepage https://github.com/craue/CraueFormFlowBundle
Informations about the package formflow-bundle
Information
CraueFormFlowBundle provides a facility for building and handling multi-step forms in your Symfony project.
Features:
- navigation (next, back, start over)
- step labels
- skipping of steps
- different validation group for each step
- handling of file uploads
- dynamic step navigation (optional)
- redirect after submit (a.k.a. "Post/Redirect/Get", optional)
A live demo showcasing these features is available at http://craue.de/symfony-playground/en/CraueFormFlow/.
Installation
Get the bundle
Let Composer download and install the bundle by running
in a shell.
Enable the bundle
If you don't use Symfony Flex, register the bundle manually:
Or, for Symfony 3.4:
Usage
This section shows how to create a 3-step form flow for creating a vehicle. You have to choose between two approaches on how to set up your flow.
Approach A: One form type for the entire flow
This approach makes it easy to turn an existing (common) form into a form flow.
Create a flow class
Create a form type class
You only have to create one form type class for a flow.
There is an option called flow_step
you can use to decide which fields will be added to the form
according to the step to render.
Approach B: One form type per step
This approach makes it easy to reuse the form types to compose other forms.
Create a flow class
Create form type classes
Register your flow as a service
XML
YAML
When not using autoconfiguration, you may let your flow inherit the required dependencies from a parent service.
XML
YAML
Create a form template
You only need one template for a flow.
The instance of your flow class is passed to the template in a variable called flow
so you can use it to render the
form according to the current step.
CSS
Some CSS is needed to render the buttons correctly. Load the provided file in your base template:
...and install the assets in your project:
Buttons
You can customize the default button look by using these variables to add one or more CSS classes to them:
craue_formflow_button_class_last
will apply either to the next or finish buttoncraue_formflow_button_class_finish
will specifically apply to the finish buttoncraue_formflow_button_class_next
will specifically apply to the next buttoncraue_formflow_button_class_back
will apply to the back buttoncraue_formflow_button_class_reset
will apply to the reset button
Example with Bootstrap button classes:
In the same manner you can customize the button labels:
craue_formflow_button_label_last
for either the next or finish buttoncraue_formflow_button_label_finish
for the finish buttoncraue_formflow_button_label_next
for the next buttoncraue_formflow_button_label_back
for the back buttoncraue_formflow_button_label_reset
for the reset button
Example:
You can also remove the reset button by setting craue_formflow_button_render_reset
to false
.
Create an action
Explanations
How the flow works
- Dispatch
PreBindEvent
. - Dispatch
GetStepsEvent
. - Update the form data class with previously saved data of all steps. For each one, dispatch
PostBindSavedDataEvent
. - Evaluate which steps are skipped. Determine the current step.
- Dispatch
PostBindFlowEvent
. - Create the form for the current step.
- Bind the request to that form.
- Dispatch
PostBindRequestEvent
. - Validate the form data.
- Dispatch
PostValidateEvent
. - Save the form data.
- Proceed to the next step.
Method loadStepsConfig
The array returned by that method is used to create all steps of the flow. The first item will be the first step. You can, however, explicitly index the array for easier readability.
Valid options per step are:
label
(string
|StepLabel
|null
)- If you'd like to render an overview of all steps you have to set the
label
option for each step. - If using a callable on a
StepLabel
instance, it has to return a string value ornull
. - By default, the labels will be translated using the
messages
domain when rendered in Twig.
- If you'd like to render an overview of all steps you have to set the
form_type
(FormTypeInterface
|string
|null
)- The form type used to build the form for that step.
- This value is passed to Symfony's form factory, thus the same rules apply as for creating common forms. If using a string, it has to be the FQCN of the form type.
form_options
(array
)- Options passed to the form type of that step.
skip
(callable
|bool
)- Decides whether the step will be skipped.
- If using a callable...
- it will receive the estimated current step number and the flow as arguments;
- it has to return a boolean value;
- it might be called more than once until the actual current step number has been determined.
Examples
Advanced stuff
Validation groups
To validate the form data class bound to the flow, a step-based validation group is passed to the form type.
By default, if the flow's getName
method returns createVehicle
, such a group is named flow_createVehicle_step1
for the first step. You can customize this name by setting the flow's property validationGroupPrefix
explicitly.
The step number (1, 2, 3, etc.) will be appended by the flow.
Compared to standalone forms, setting the validation_groups
option in your form type's configureOptions
method won't have any effect in the context of a flow. The value is just ignored, i.e. will be overwritten by the flow.
But there are other ways of defining custom validation groups:
- override the flow's
getFormOptions
method, - use the
form_options
step option, or - use the flow's
setGenericFormOptions
method.
The generated step-based validation group will be added by the flow, unless the validation_groups
option is set to false
, a closure, or a GroupSequence.
In this case, it will not be added by the flow, so ensure the step forms are validated as expected.
Disabling revalidation of previous steps
Take a look at #98 for an example on why it's useful to revalidate previous steps by default. But if you want (or need) to avoid revalidating previous steps, add this to your flow class:
Passing generic options to the form type
To set options common for the form type(s) of all steps you can use method setGenericFormOptions
:
Passing step-based options to the form type
To pass individual options to each step's form type you can use the step config option form_options
:
Alternatively, to set options based on previous steps (e.g. to render fields depending on submitted data) you can override method
getFormOptions
of your flow class:
Enabling dynamic step navigation
Dynamic step navigation means that the step list rendered will contain links to go back/forth to a specific step (which has been done already) directly. To enable it, add this to your flow class:
If you'd like to remove the parameters (added by using such a direct link) when submitting the form you should modify the action for the opening form tag in the template like this:
Handling of file uploads
File uploads are transparently handled by Base64-encoding the content and storing it in the session, so it may affect performance. This feature is enabled by default for convenience, but can be disabled in the flow class as follows:
By default, the system's directory for temporary files will be used for files restored from the session while loading step data. You can set a custom one:
Enabling redirect after submit
This feature will allow performing a redirect after submitting a step to load the page containing the next step using a GET request. To enable it, add this to your flow class:
But you still have to perform the redirect yourself, so update your action like this:
Using events
There are some events which you can subscribe to. Using all of them right inside your flow class could look like this:
All versions of formflow-bundle with dependencies
symfony/config Version ~3.4|~4.4|~5.1
symfony/dependency-injection Version ~3.4|~4.4|~5.1
symfony/event-dispatcher Version ~3.4|~4.4|~5.1
symfony/form Version ~3.4|~4.4|~5.1
symfony/http-foundation Version ~3.4|~4.4|~5.1
symfony/http-kernel Version ~3.4|~4.4|~5.1
symfony/options-resolver Version ~3.4|~4.4|~5.1
symfony/security-core Version ~3.4|~4.4|~5.1
symfony/translation Version ~3.4|~4.4|~5.1
symfony/validator Version ~3.4|~4.4|~5.1
symfony/yaml Version ~3.4|~4.4|~5.1