Sub topic 6
Showing errors users can fix
This chapter assumes you have read the earlier chapters on flow state and per-step validation. You should know that validate returns a list of problems and that advance stays on the current step when that list is not empty. Here you will change what happens to that list: how it is shaped, worded, and shown.
What makes an error fixable
A user can fix an error when it answers three questions:
- Where is the problem?
- What is wrong?
- What would make it right?
"Invalid input" answers none of them. "Enter an email address like [email protected]" answers all three.
| Weak message | Fixable message |
|---|---|
| Invalid input | Enter an email address like [email protected]. |
| Password error | Use at least 12 characters. You entered 8. |
| Required | Enter your company name. |
State the rule before the user breaks it when you can. A hint under the field ("At least 12 characters") prevents many errors. Messages also must not blame the user ("You forgot...") or expose internal codes.
Tie each error to a field
The earlier examples return plain strings. That works for one message per step. Once a step has several fields, a string cannot say which field to highlight or focus. Return a field name with each message.
type Data = Record<string, string>;
type FieldError = { field: string; message: string };
function validateAccount(d: Data): FieldError[] {
const errors: FieldError[] = [];
if (!/^\S+@\S+\.\S+$/.test(d.email ?? "")) {
errors.push({
field: "email",
message: "Enter an email address like [email protected].",
});
}
const length = (d.password ?? "").length;
if (length < 12) {
errors.push({
field: "password",
message: "Use at least 12 characters. You entered " + length + ".",
});
}
return errors;
}
function errorSummary(errors: FieldError[]): string {
if (errors.length === 0) return "";
const count = errors.length === 1 ? "1 problem" : errors.length + " problems";
return "Fix " + count + " to continue.";
}
function clearFieldError(errors: FieldError[], field: string): FieldError[] {
return errors.filter((e) => e.field !== field);
}
const errors = validateAccount({ email: "ana@", password: "short" });
console.log(errorSummary(errors));
console.log(errors.map((e) => e.field + ": " + e.message));
console.log(clearFieldError(errors, "email").length);In FlowState, change errors: string[] to errors: FieldError[]. The advance function does not change, because it only checks the length of the list.
Show errors in two places
Show each message next to its field, and show a short summary at the top of the step. The summary helps users who cannot see the whole screen, and screen reader users hear it first. Order the summary in the same order as the fields on screen, and make each entry link to its field.
Move focus to the first invalid field after a failed attempt. Without that, a keyboard user stays on the button and has to hunt for the problem.
Recover without losing work
Recovery matters as much as the message. Follow these rules:
- Keep everything the user typed. Never clear a field because it failed validation.
- Clear a field's error as soon as the user edits it, not on the next submit. Stale errors make users think their fix did not work.
- Do not show errors before the user has tried. Validate on submit first. After that, you can validate a field when the user leaves it.
- Keep Back working.
backalready setserrors: [], so a user who returns to an earlier step does not see problems from a step they left.
Separate user errors from system errors
Validation errors are the user's to fix. A failed network call or a server timeout is not. Do not show "Enter a valid value" when the server was unreachable. Keep a separate kind of error for system failures. Say what happened, say that the data is still saved in the form, and offer a retry action.
Server-side checks, such as "that email is already registered", should come back in the same FieldError shape. The interface then renders them the same way as client-side errors.
Common mistakes
- Showing only the first error, which forces users to fix and resubmit once per problem.
- Showing errors only in color. Add text, and an icon if you like.
- Using placeholder text as the only instruction. It disappears once the user types.
- Disabling the Next button instead of explaining what is missing.
Exercise
A user submits a step with a bad email and a short password, fixes the email, and presses Next again. Which error list should the flow hold at that moment, and which messages should be visible?
Show answer
Fixing the email calls clearFieldError for "email", so the held list contains only the password error. The email message disappears as soon as the user edits it. Pressing Next then runs validation again, and the password error stays, so the summary reads "Fix 1 problem to continue." and focus moves to the password field.