Mastering Angular Form Groups: Building Dynamic User Inputs
Angular's FormGroup is a powerful tool for managing complex forms, allowing you to group multiple form controls, track their validity, and react to changes. This challenge will test your understanding of FormGroup by having you build a dynamic registration form where fields can be conditionally added or removed.
Problem Description
Your task is to implement an Angular component that displays a user registration form. The form should allow users to input basic information (email, password) and optionally add additional fields like a username. The addition and removal of these optional fields should be dynamically managed using Angular's FormGroup and FormArray.
Key Requirements:
- Base Form Controls: The form must include
emailandpasswordas required form controls within aFormGroup. - Dynamic Field Addition: Implement a button that allows the user to add an optional
usernamefield to the form. - Dynamic Field Removal: Implement a way to remove the added
usernamefield. Each addedusernamefield should have its own removal button. - Form Structure: Utilize
FormGroupto manage the overall form structure andFormArrayto manage the collection of optionalusernamefields. - Validation:
emailshould be a required field.passwordshould be a required field.usernameshould be an optional field, but if present, it should not be empty.
- Submission: A submit button should be present. When clicked, the component should log the current form value to the console.
Expected Behavior:
- The form initially displays email and password fields.
- Clicking "Add Username" adds a username input field and a "Remove Username" button next to it.
- The user can add multiple username fields (though for simplicity, we'll focus on one initially, but the solution should be scalable).
- Clicking "Remove Username" removes the corresponding username input and its button.
- The form can be submitted, logging its data. Basic validation should prevent submission if required fields are empty.
Edge Cases:
- Attempting to submit the form with empty required fields.
- Adding and removing username fields multiple times.
- The impact of adding/removing fields on the overall form's validity.
Examples
Example 1: Initial Form State
- Input: User opens the registration form.
- Output: The form displays input fields for "Email" and "Password". A button labeled "Add Username" is visible.
- Explanation: This is the default state of the form before any dynamic fields are added.
Example 2: After Adding a Username
- Input: User clicks the "Add Username" button.
- Output: The form now displays "Email", "Password", and "Username" input fields. A "Remove Username" button is visible next to the username field.
- Explanation: The
FormArrayhas been updated to include a newFormControlfor the username, and the template reflects this change.
Example 3: After Removing a Username
- Input: User clicks the "Remove Username" button (after adding one).
- Output: The form reverts to displaying only "Email" and "Password" input fields. The "Add Username" button reappears.
- Explanation: The
FormArrayhas been updated by removing the usernameFormControl, and the template reflects this change.
Constraints
- Angular version: Latest stable version (e.g., 16, 17).
- TypeScript version: Compatible with the Angular version.
- Use
@angular/formsmodule exclusively for form management. - No external form libraries are allowed.
- The solution should be a single Angular component.
Notes
- Consider how you will structure your
FormGroupto accommodate a variable number of username fields.FormArrayis the key here. - Think about how to update the template dynamically to render the username fields based on the
FormArray. - The
valueChangesobservable on form controls or the form group itself can be useful for observing changes. - Start by setting up the basic
FormGroupwith email and password, then tackle the dynamicFormArrayintegration.