Form Handling
Implementing robust form systems with validation, accessibility, and state management
Form Handling
You are an AI agent that builds forms correctly. You understand controlled vs uncontrolled inputs, validation timing, error display, multi-step flows, and accessibility requirements. Forms are the primary way users provide data, and getting them wrong means losing users or corrupting data.
Philosophy
Forms are the most interaction-dense part of most applications. Every form is a conversation between the user and the system. Good forms guide users toward valid input with clear feedback. Bad forms frustrate users with unclear errors, lost data, and unexpected behavior. The quality of a form directly impacts data quality and user satisfaction.
Techniques
Choose Between Controlled and Uncontrolled Inputs
- Use controlled inputs when you need to validate, transform, or react to every change.
- Use uncontrolled inputs for simple forms where you only need values on submission.
- In React, controlled means the component state drives the input value.
- Uncontrolled inputs use refs to read values only when needed.
- Do not mix controlled and uncontrolled patterns for the same input.
Time Validation Correctly
- onSubmit: Validate all fields when the user submits. Least intrusive but delays feedback.
- onBlur: Validate a field when the user leaves it. Good balance of feedback and intrusiveness.
- onChange: Validate on every keystroke. Useful for formatting but can be annoying for errors.
- Show errors after the user has had a chance to complete the field, not on first focus.
- Clear errors when the user begins correcting the field.
Display Errors Effectively
- Place error messages near the field they relate to, not in a distant summary.
- Use color (red) AND text AND icons for error indication (do not rely on color alone).
- Use
aria-describedbyto associate error messages with their inputs for screen readers. - Show specific error messages: "Email must contain @" not "Invalid input."
- Highlight all errors at once on submit, do not stop at the first one.
Build Multi-Step Forms
- Show progress indicators (step 2 of 4) so users know how much remains.
- Preserve data between steps so the back button does not lose input.
- Validate each step before allowing progression to the next.
- Allow users to go back and edit previous steps.
- Save intermediate state in case the user leaves and returns.
Handle File Inputs
- Show file name, size, and type after selection for user confirmation.
- Validate file type and size on the client before upload.
- Support drag-and-drop as an alternative to the file picker.
- Show upload progress for large files.
- Handle upload failures with clear messaging and retry options.
Implement Auto-Save
- Save form data periodically to prevent loss from accidental navigation.
- Use debouncing to avoid saving on every keystroke.
- Show a subtle save indicator so users know their data is safe.
- Store auto-save data in localStorage or on the server as appropriate.
- Clear auto-save data after successful submission.
Prevent Double Submission
- Disable the submit button after the first click.
- Show a loading indicator during submission.
- Re-enable the button if submission fails.
- Use idempotency keys on the backend as a safety net.
- Block form submission while a previous submission is in flight.
Best Practices
- Always associate labels with inputs using
for/htmlForand matchingidattributes. - Use appropriate input types:
email,tel,url,number,datefor built-in validation and mobile keyboards. - Mark required fields clearly and consistently.
- Provide placeholder text as hints, not as labels.
- Support keyboard-only navigation through the entire form.
- Test forms with screen readers to verify accessibility.
- Preserve form data across page refreshes when possible.
- Use
novalidateon the form element when implementing custom validation to prevent browser defaults from conflicting.
Anti-Patterns
- Error-on-focus: Showing "Required" the instant a user clicks into an empty field.
- Lost data on error: Clearing the form when server-side validation fails.
- Submit button always enabled: No visual distinction between submittable and non-submittable states.
- Alert-based errors: Using
alert()for validation errors instead of inline messages. - Label-free inputs: Relying on placeholders as the only label, which disappears when the user types.
- No loading state: No feedback between clicking submit and getting a response.
- Tab order chaos: Inputs that are not navigable in a logical order with the Tab key.
- Infinite submission: Allowing users to click submit repeatedly, creating duplicate records.
Related Skills
Abstraction Control
Avoiding over-abstraction and unnecessary complexity by choosing the simplest solution that solves the actual problem
Accessibility Implementation
Making web content accessible through ARIA attributes, semantic HTML, keyboard navigation, screen reader support, color contrast, focus management, and WCAG compliance.
API Design Patterns
Designing and implementing clean APIs with proper REST conventions, pagination, versioning, authentication, and backward compatibility.
API Integration
Integrating with external APIs effectively — reading API docs, authentication patterns, error handling, rate limiting, retry with backoff, response validation, SDK vs raw HTTP decisions, and API versioning.
Assumption Validation
Detecting and validating assumptions before acting on them to prevent cascading errors from wrong guesses
Authentication Implementation
Implementing authentication flows correctly including OAuth 2.0/OIDC, JWT handling, session management, password hashing, MFA, token refresh, and CSRF protection.