I have one query in Angular Reactive Form.
I have below formGroup, and inside that formGroup, I have a formArray, and inside that formArray, I have another formGroup. Here is the main formGroup
public formGroup = this.formBuilder.group({
email: ["", Validators.required],
address: this.formBuilder.array([
this.initFun()
])
});
Here is the formArray initFun() function
fun() {
return this.formBuilder.group({
city: [""],
state: [""]
});
}
Now the query is I want to set the validator to city and state dynamically.
What is the approach to add a dynamic validator to formArray inside formGroup?
I can set the validator to email using below syntax:
this.formGroup.controls["email"].setValidators(Validators.required);
But I am not able to set validator for the city and state inside the address array.
Any help would be highly appreciated :)
Update
I've created a sample of the above case you can check out here: https://stackblitz.com/edit/reactive-form-angular
Let me know if I am doing something wrong here.