{"success":true,"name":"on-form-errors-keep-what-the-user-typed-mark-the-field-expla","description":"Building a form whose submit can fail validation or hit a server error","category":"ai-brimind","path":"ai-brimind/on-form-errors-keep-what-the-user-typed-mark-the-field-expla/SKILL.md","file":"SKILL.md","content":"---\nname: on-form-errors-keep-what-the-user-typed-mark-the-field-expla\ndescription: \"Building a form whose submit can fail validation or hit a server error\"\n---\n\n# On Form Errors, Keep What the User Typed: Mark the Field, Explain the Fix, Never Wipe the Work\n\n## When to use\nYou are building or wiring a frontend form (signup, create club, checkout) and the failure path currently clears inputs, shows one vague global message, or navigates away — losing the user's typed work when validation or the server rejects the submit.\n\n## Example request\nA 12-field registration fails on one bad phone number and the form resets → the user retypes everything or leaves forever; inputs are state the user OWNS, and failure must never erase their work. Server returns 'validation error' and the UI shows a red banner saying only 'Error' → the user cannot tell WHICH field or WHY; your API error contract carries the field name and reason precisely so the form can mark THAT input with THAT message. Errors shown while the user is still typing their email → yelling 'invalid email' at 'jo' is noise; validate on blur or on submit, then clear the error the moment the field is corrected — timing is part of the design. The submit failed from a network drop → nothing was wrong with the data; the form needs a 'could not send, try again' state distinct from field errors, with everything preserved for one-click retry.\n\n## Instructions\nAct as a frontend developer who treats a filled-in form as the user's unsaved work — five minutes of their life sitting in your inputs — and any code path that discards it as equivalent to a text editor deleting a document because saving failed. For every form you build, do this: (1) Lock the invariant first: on ANY failure — client validation, server rejection, network error — every input keeps its value. In React that simply means the state holding the values is never reset in error paths (reset belongs exclusively to the success path, and even there decide deliberately: navigating away vs clearing). Audit each failure branch for this before styling anything. (2) Route errors to their homes: your API error contract returns which field and why — map it so each failing input gets marked (visual state + the specific message beside it) and the first invalid field is scrolled to and focused, because on a long form the user must not hunt for the red. A global banner is only for errors that belong to no field (network down, server fault). (3) Design the TIMING of validation like UX, not policing: validate a field on blur or on submit — not on every keystroke of a half-typed value — and clear its error immediately once corrected, so the form guides instead of nags. Keep the client-side checks mirrored to the border validation, and remember which one is the law: the client copy is comfort; the server's answer is the verdict, and the form must render server-sent field errors just as gracefully as its own. (4) Distinguish 'your data is wrong' from 'the send failed': a network drop or 500 gets a retry affordance with all data intact and the submit button re-enabled per your pending-state discipline — showing field-error styling for a network problem sends the user hunting a mistake they did not make. (5) Test the failure paths as the primary paths, because for forms they are: submit with one bad field (everything preserved? the field marked? focus moved?), submit with the backend down (retry offered? data intact?), fix the field and resubmit (old error gone?). Only call the form done when the most careless failure path preserves every keystroke, names every problem at its source, and lets the user recover with the minimum possible re-work — a form that punishes mistakes by deleting effort is not validating input, it is losing users.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #227\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}