Symfony 8.2 New Published on

Grouping the steps of a multi-step form

Symfony 8.2 nests the steps of a form flow: createStepGroup() brings the company and billing steps together, and a single setSkip() skips both for an individual.

Topics covered: Forms

New in Symfony 8.2 (in development). The form flows of Symfony 7.4 split a form into steps, but on a single level. To skip several steps at once, you had to repeat the same condition on each of them.

Steps inside a step

In 8.2, a step can contain other steps. When it only exists to group them, create it with createStepGroup():

$builder
    ->addStep(
        $builder->createStepGroup('flights')
            ->addStep('outbound', OutboundFlightType::class)
            ->addStep('return', ReturnFlightType::class)
    )
    ->addStep('passengers', PassengersType::class)
    ->addStep('payment', PaymentType::class);
  • A group renders no form: the flow goes straight to its first step.
  • setSkip() on a step skips it along with all its child steps, based on the data already entered.
  • Nesting only changes the structure and the navigation: all the steps fill the same object.

In the template, form.vars.steps gives the tree of steps, with children and is_group for each one. form.vars.visible_steps keeps only the top-level steps that aren't skipped, and visible_children does the same for each step's children.

Your task

Signing up for a training course goes through four steps: profile, company, billing and summary. Yet an individual has no company or billing address to give.

  1. Bring the company and billing steps together in a business step group, between the profile and the summary.
  2. Skip the whole group when the sign-up isn't for a business, with a single condition.

Try the flow in the preview, as an individual and then as a business.

Where the feature comes from: the original pull request.