Building a Simple Form with Angular's FormsModule
This challenge focuses on implementing Angular's FormsModule to create and handle a basic form. Understanding form handling is crucial for building interactive user interfaces, allowing users to input data and triggering actions based on that data. You'll be building a form with input fields, validation, and a submission handler.
Problem Description
You are tasked with creating a simple Angular component that displays a form with the following fields:
- Name: A text input field (
name) required. - Email: An email input field (
email) required and must be a valid email format. - Age: A number input field (
age) required and must be a positive integer.
The component should:
- Use Angular's
FormsModuleto enable two-way data binding and form validation. - Display appropriate error messages next to each input field when validation fails. Error messages should be clear and user-friendly (e.g., "Name is required", "Invalid email format", "Age must be a positive integer").
- Have a "Submit" button.
- When the form is submitted (and valid), log the form data (name, email, age) to the console.
- Prevent the default form submission behavior (page refresh).
Expected Behavior:
- The form should initially be invalid if any required fields are empty or invalid.
- As the user enters data, the form's validity should update in real-time.
- Error messages should appear next to the corresponding input fields when they are invalid.
- The "Submit" button should be disabled when the form is invalid.
- Upon successful submission (valid form), the form data should be logged to the console, and the form should remain visible.
Examples
Example 1:
Input: Form with empty fields and invalid email.
Output: Error messages displayed next to 'Name', 'Email', and 'Age' fields. Submit button disabled.
Explanation: The form is invalid because all required fields are empty or invalid.
Example 2:
Input: Form with valid data (e.g., Name: "John Doe", Email: "john.doe@example.com", Age: 30).
Output: No error messages displayed. Submit button enabled. Console logs: { name: "John Doe", email: "john.doe@example.com", age: 30 }
Explanation: The form is valid, and the data is logged to the console upon submission.
Example 3:
Input: Form with Name: "", Email: "invalid-email", Age: -5
Output: Error messages displayed next to 'Name', 'Email', and 'Age' fields. Submit button disabled.
Explanation: Demonstrates multiple validation errors simultaneously.
Constraints
- You must use Angular's
FormsModule. - The component should be a standalone component.
- Validation should be implemented using Angular's built-in validators (e.g.,
Validators.required,Validators.email,Validators.min). - The component should be well-structured and readable.
- The component should handle the submission event correctly, preventing page refresh.
Notes
- Consider using
ngModelfor two-way data binding. - Utilize Angular's form control directives (e.g.,
required,email,min) for validation. - Use
formControlandvalidationMessagesto display error messages dynamically. - Remember to import
FormsModulein your module. - Think about how to handle the form submission event and prevent the default behavior.
- You can use a simple template-driven form approach for this challenge.