Hone logo
Hone
Problems

Angular POST Request with Data Validation and Error Handling

This challenge focuses on implementing a POST request in an Angular application to send data to a backend server. It's a fundamental skill for building interactive web applications that need to communicate with APIs, and this exercise emphasizes data validation and robust error handling to ensure a reliable user experience.

Problem Description

You are tasked with creating an Angular component that allows a user to input data (name and email) and submit it to a backend API endpoint via a POST request. The component should:

  1. Collect User Input: Provide two input fields: one for the user's name (string) and one for their email address (string).
  2. Data Validation: Validate the email address format using a regular expression before sending the request. The email must contain an "@" symbol and a "." symbol. Display an error message to the user if the email is invalid.
  3. POST Request: Upon successful validation and submission, send a POST request to a predefined API endpoint (/api/users). The request body should be a JSON object containing the name and email values entered by the user.
  4. Success Handling: If the POST request is successful (status code 200-299), display a success message to the user.
  5. Error Handling: If the POST request fails (status code outside 200-299), display an appropriate error message to the user, indicating that the submission failed. Include the error status code in the message.
  6. Clear Input Fields: After a successful submission, clear the input fields.

Examples

Example 1:

Input: Name: "John Doe", Email: "john.doe@example.com"
Output: Success message: "User John Doe successfully registered!" Input fields cleared.
Explanation: The email is valid, the POST request succeeds, and the user is notified.

Example 2:

Input: Name: "Jane Smith", Email: "jane.smith"
Output: Error message: "Invalid email format. Please enter a valid email address." Input fields remain unchanged.
Explanation: The email is invalid, the POST request is not sent, and the user is prompted to correct the email.

Example 3:

Input: Name: "Peter Jones", Email: "peter.jones@example.com"
Output: Error message: "Submission failed. Status code: 500" Input fields remain unchanged.
Explanation: The email is valid, the POST request fails with a 500 error, and the user is notified of the failure and the status code.

Constraints

  • The API endpoint is /api/users. Assume this endpoint exists and accepts JSON data.
  • The email validation regular expression must be reasonably accurate (e.g., ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$).
  • The Angular component should be well-structured and follow best practices.
  • Assume the backend will return a JSON response, even on error. The response body is not critical for this challenge, but you should handle it gracefully.
  • You can use Angular's HttpClient module for making the POST request.

Notes

  • Consider using Angular's reactive forms for managing the input fields and validation.
  • Use appropriate error handling techniques (e.g., try...catch blocks) to catch potential errors during the POST request.
  • Think about how to provide clear and informative error messages to the user.
  • Focus on creating a robust and user-friendly component that handles both successful and unsuccessful submissions gracefully.
  • You don't need to implement the backend API itself; focus solely on the Angular component. You can mock the API response for testing purposes.
Loading editor...
typescript