I'm trying to figure out how to create an angular form with a dynamic step. Currently, my form in TypeScript looks like this:
this.registerForm = new FormGroup({
role: new FormControl('', [
Validators.required,
]),
firstName: new FormControl('', [
Validators.required,
Validators.minLength(2),
Validators.maxLength(20),
]),
lastName: new FormControl('', [
Validators.required,
Validators.minLength(2),
Validators.maxLength(20),
]),
email: new FormControl('', [
Validators.required,
Validators.email
])
});
Let's say that the "role" FromControl (the first one) allows for 2 different values (x and y). How can I add a new field to my form that changes based on the selected role?
I was thinking of having buttons to select the role and then show the dynamic field at the bottom of the form. Depending on the role, I want to display it either as a dropdown or a chip component (both from Angular Material).
I don't think using two separate forms with big "ngIf" blocks is a good idea, as the user might start typing before changing their role between x and y.
Would creating a second form for the dynamic field be a better approach?
Appreciate any advice! Kev.