Robust Form Handling with Error Recovery in Angular
Angular forms are crucial for user input, but unexpected errors can disrupt the user experience. This challenge focuses on implementing robust error recovery within an Angular form, allowing the application to gracefully handle errors, provide informative feedback to the user, and potentially allow them to correct their input without losing progress. The goal is to create a form that doesn't simply break on error, but actively guides the user towards a solution.
Problem Description
You are tasked with building an Angular component that implements a form with error recovery. The form should have at least three fields: name, email, and age. The name field is required. The email field must be a valid email address. The age field must be a number between 18 and 100 (inclusive).
The component should:
- Validate Form Fields: Implement both synchronous and asynchronous validation for each field. Synchronous validation should check for required fields and basic format (e.g., name not empty). Asynchronous validation should check for email validity (e.g., against a dummy API – a simple
setTimeoutsimulating a network call is acceptable for this challenge). - Handle Validation Errors: Display clear and concise error messages to the user for each invalid field. These messages should be displayed immediately upon losing focus of a field or attempting to submit the form.
- Implement Error Recovery: When an error occurs during asynchronous validation (e.g., email validation fails), the form should not prevent the user from continuing to interact with other fields. The user should be able to correct the email address and then proceed to validate other fields.
- Submit Form: Upon successful validation of all fields, the form should submit the data. For this challenge, simply log the data to the console.
- Reset Form: Provide a button to reset the form to its initial state, clearing all values and error messages.
Expected Behavior:
- The form should initially be invalid if any required fields are empty or if any validation rules are violated.
- Error messages should appear immediately when a field is invalid.
- Asynchronous validation errors should not block the user from interacting with other fields.
- The form should only submit when all validations pass.
- The reset button should clear all form data and error messages.
Edge Cases to Consider:
- Empty input strings.
- Invalid email formats.
- Age values outside the allowed range.
- Simulated asynchronous validation failures (e.g., network errors).
- User rapidly switching between fields with different validation states.
Examples
Example 1:
Input: Name: "", Email: "invalid-email", Age: "abc"
Output: Error messages displayed for Name (required), Email (invalid format), Age (not a number). Form remains invalid.
Explanation: All fields are invalid, triggering immediate error messages. The form does not submit.
Example 2:
Input: Name: "John Doe", Email: "john.doe@example.com", Age: "25"
Output: No error messages displayed. Form is valid. Submitting the form logs: { name: "John Doe", email: "john.doe@example.com", age: 25 } to the console.
Explanation: All fields are valid, allowing form submission.
Example 3:
Input: Name: "Jane Doe", Email: "jane.doe@example.com", Age: "17"
Output: Error message displayed for Age (must be between 18 and 100). Form remains invalid.
Explanation: The age is invalid, triggering an error message. The form does not submit.
Constraints
- Angular Version: Use Angular 14 or later.
- Reactive Forms: You must use Angular's Reactive Forms approach (not Template-Driven Forms).
- Asynchronous Validation: The email validation must be asynchronous, simulating a network request with
setTimeout. The timeout should be between 500ms and 1500ms. - Error Message Clarity: Error messages should be user-friendly and clearly indicate the problem.
- Performance: The component should remain responsive even with asynchronous validation. Avoid blocking the UI thread.
- Component Structure: The solution should be a single, well-structured Angular component.
Notes
- Consider using Angular's
ValidatorsandValidationErrorsto manage validation rules and error messages. - Utilize Angular's
FormControlandFormGroupto define and manage the form structure. - Think about how to handle the asynchronous validation process and provide feedback to the user while the validation is in progress. A loading indicator might be helpful, but is not required.
- Focus on creating a robust and user-friendly form that gracefully handles errors and allows for easy error recovery. The simulated API call is just to demonstrate asynchronous validation; the actual API interaction is not required.
- The reset button should clear all form values and error messages.