APP-19Required
Never lose what people typed
Keep every value people entered after a failed submit, a lost connection, an expired session or an accidental reload. Never clear a form unless it was saved.
How to do it
After a failed submit, leave the fields as they are, or render them again with the submitted values on the server. Save a draft of long forms, with more than 5 fields or any free-text area, to sessionStorage or the server on every change. Restore it on load. Never store passwords or card numbers in a draft. Clear the draft only after the server confirms the save.
Examples
TypeScript example
const DRAFT_KEY = `draft:${location.pathname}`;
const SECRET_FIELDS = new Set(['password', 'cardNumber']);
export function keepDraft(form: HTMLFormElement) {
const saved = JSON.parse(sessionStorage.getItem(DRAFT_KEY) ?? '{}');
for (const [name, value] of Object.entries(saved)) {
const field = form.elements.namedItem(name);
const isTextField = field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement || field instanceof HTMLSelectElement;
if (isTextField && field.type !== 'file') field.value = String(value);
}
form.addEventListener('input', () => {
const values = Object.fromEntries([...new FormData(form)].filter(([name, value]) => typeof value === 'string' && !SECRET_FIELDS.has(name)));
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(values));
});
}
export const clearDraft = () => sessionStorage.removeItem(DRAFT_KEY);
Python example
@app.post("/invoices")
def create_invoice():
try:
invoice = InvoiceInput.model_validate(request.form.to_dict())
except ValidationError as error:
return render_template("invoices/new.html", values=request.form, errors=field_errors(error)), 422
saved = invoices.create(invoice)
return redirect(url_for("show_invoice", invoice_id=saved.id), code=303)
How to check
A browser test fills a form with more than 5 fields, reloads the page and finds the values again. A second test submits with a 500 from the server, and every field still holds its value.
Sources
Related rules
- APP-58 Never save over a newer version in Web app interface
- APP-60 Expect browser storage to fail or be cleared in Web app interface
- APP-67 Let people check and change what a model filled in in Web app interface
- AXS-32 Never ask for the same thing twice in Accessibility
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/forms/keep-user-input">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.