Question presented to candidate: "Build a contact form with Name, Email and Message. Show helpful error messages, but do not yell at people before they have had a chance to type. When they submit, send it to a fake API and show a thank-you screen. No form libraries, please."
Requirements:
- Name (required, at least 2 characters), Email (required, valid format), Message (required, 10 to 200 characters, with a live character count).
- An error appears after the field loses focus (blur) or after a submit attempt, and then updates live as the user types.
- On submit with errors: show every error, a short summary ("Please fix 2 fields."), and move focus to the first invalid field.
- On a valid submit: disable the button while a simulated request runs, map a server-side field error back onto its field, and show a success state with a way to start over.
- Errors are announced to assistive technology and tied to their inputs.
What a strong answer should cover:
- A single source of truth for values, with errors derived from values by a pure
validate()function rather than stored and kept in sync by hand. - Touched and submitted flags that decide when an error is visible, separate from whether the field is invalid.
- Controlled inputs (or
v-model/ reactive forms),noValidateon the form, and preventing the default submit. - Accessibility:
<label for>,aria-invalid,aria-describedbypointing at the error text, a live region for the summary, focus on the first invalid field. - Server errors as a second error source that clears when the user edits that field; a guard against double submits.
Clarifying questions expected:
- "Should errors show while typing, on blur, or only on submit?"
- "Should the submit button be disabled until the form is valid, or always clickable?"
- "Are the rules fixed, or do they come from the server or a schema?"
Code / implementation expected: Yes. A working form component with a pure validation function, touched/submitted tracking, accessible error markup and a simulated async submit.