Sub topic 8
Modeling a Multi-Step Flow as a State Machine
Assume you know basic HTML form elements, JavaScript or TypeScript, and how a browser submits form data. This chapter focuses on the flow itself: which step is active, which actions are allowed, and how validation controls movement.
A multi-step form is easier to reason about when you model it as a state machine. A state machine is a set of named states and the allowed transitions between them. For a form, a state might be account, details, or review. An action such as next, back, or change causes a transition.
Define states, actions, and guards
Keep the step list explicit. Each transition should have a clear rule, called a guard. For example, the next action can move from account to details only when the account fields are valid.
| Part | Form example | Purpose |
|---|---|---|
| State | `account` | Identifies the current step |
| Action | `{ type: "next" }` | Describes what the user wants to do |
| Guard | `validate("account")` | Decides whether a transition is allowed |
| Transition | `account → details` | Changes the current step |
The form should store values and errors separately from the current step. Going back changes the step, but it should not erase values.
const steps = ["account", "details", "review"] as const;
type StepId = (typeof steps)[number];
type State = {
step: StepId;
values: Record<string, string>;
errors: Record<string, string>;
};
type Action =
| { type: "change"; name: string; value: string }
| { type: "next" }
| { type: "back" };
function validate(
step: StepId,
values: Record<string, string>
): Record<string, string> {
const errors: Record<string, string> = {};
if (step === "account" && !values.email?.includes("@")) {
errors.email = "Enter an email address in the format [email protected].";
}
if (step === "details" && !values.name?.trim()) {
errors.name = "Enter your full name.";
}
return errors;
}
function reducer(state: State, action: Action): State {
const index = steps.indexOf(state.step);
switch (action.type) {
case "change":
return {
...state,
values: { ...state.values, [action.name]: action.value },
};
case "next": {
const errors = validate(state.step, state.values);
if (Object.keys(errors).length > 0) {
return { ...state, errors };
}
return {
...state,
errors: {},
step: steps[Math.min(index + 1, steps.length - 1)],
};
}
case "back":
return {
...state,
errors: {},
step: steps[Math.max(index - 1, 0)],
};
}
}The reducer is a pure function. Given the same state and action, it returns the same next state without changing the original object. This makes transitions predictable and easy to test.
Draw the transition model
A diagram exposes missing paths before they become interface bugs. In this flow, next from account is guarded by account validation. The review step has no later step, so its primary action is submission rather than another transition.
Treat browser navigation as part of the model. If a user reloads the page, restore a saved state only after checking that its step and values are valid. Otherwise, start at the first step.
Test transitions, not only screens
Test each action from each relevant state:
- Valid input plus
nextmoves forward. - Invalid input plus
nextstays on the same step and exposes errors. backpreserves entered values.backon the first step does not produce an invalid step.- A reload restores a valid draft or starts safely when the draft is corrupt.
Exercise
A user completes the details step, goes back to account, changes the email address, and returns to details. Which data should the state machine preserve, and when should details be validated again?
Show answer
It should preserve both the changed email and the existing details values. It should validate details again when the user tries to move forward from details, because its transition guard is being evaluated again.