Multi-step wizard
Step(id, s => s.Title(...).Fields(...)) groups fields into steps, and BormView renders a
stepper with Back and Next. Each step validates before advancing, and steps can be shown conditionally — the
company step here only appears for business accounts. The fields are declared once, as on any form; the steps
only say which of them go where.
Press Next on an empty step: it stays put and shows what is missing. Choose Business and a
Company step appears, numbered in its place. Walk to the last step and then click back through the
stepper. Every step you have already been past stays clickable in both directions — it was validated
on the way through, so making you press Next through the rest just to fix one answer would be busywork. A step
you have never reached stays inert. Enter in a text box moves to the next step, as Next does, rather than
submitting a form the user is halfway through.
View source
@page "/wizard"
<BormView Definition="_schema" Model="_model" OnValidSubmit="HandleSubmit" SubmitText="Finish" />
@code {
private readonly AccountSignup _model = new();
private BormDefinition _schema = default!;
protected override void OnInitialized()
{
_schema = BormBuilder.For<AccountSignup>()
.Title("Account setup")
.Field(x => x.FullName, f => f.Required().MinLength(2))
.Field(x => x.Email, f => f.Required().Email())
.Field(x => x.AccountKind, f => f.AsRadio().Label("Account type"))
.Field(x => x.CompanyName, f => f.Label("Company name").Required("Required for business accounts."))
.Field(x => x.VatNumber, f => f.Label("VAT number"))
.Field(x => x.AcceptTerms, f => f.Required("Please accept the terms."))
.Step("contact", s => s.Title("Contact").Fields("FullName", "Email"))
.Step("type", s => s.Title("Account type").Fields("AccountKind"))
// Only for business accounts. While hidden, the step's fields are neither shown nor
// validated, and the steps after it renumber themselves.
.Step("company", s => s.Title("Company")
.Fields("CompanyName", "VatNumber")
.VisibleWhen("AccountKind", BormConditionOperator.Equals, "Business"))
.Step("confirm", s => s.Title("Confirm").Fields("AcceptTerms"))
.Build();
}
private void HandleSubmit(BormState state)
{
var json = System.Text.Json.JsonSerializer.Serialize(_model,
new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
}
}Worth knowing
- A hidden step is hidden in every sense. Its fields are not rendered, not validated on submit, and never the reason a form refuses to submit. If a condition hides the step the user is standing on, the wizard moves to the nearest visible one rather than stranding them.
- Focus follows the step. Changing step moves focus to the new step's content, so the change is not invisible to a keyboard or screen-reader user. A failed submit's error summary links to the step each error is on.
- Its words and its links are yours.
BackText,NextTextandSubmitTextrelabel the buttons,StepperLabelnames the stepper for assistive technology, andAllowStepNavigation="false"turns the stepper's links off. - Driving it from code.
NextStepAsync(),PreviousStep(),GoToStep(i),VisibleSteps,CurrentStepNumber,FurthestStepIndexandIsStepReachable(i)on the state are what a stepper of your own is built from. - Other ways to show the same steps.
StepLayout(BormStepLayout.Tabs),AccordionorSectionslet the user move around freely, and a review step reads every answer back - see tabs, sections & review. A step's condition can also be an expression,.VisibleWhen("{AccountKind} = 'Business'"), and in JSON Schema steps travel asx-steps.