I have the following form:
registerLibrarianForm = this.formBuilder.group({
firstName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(128), Validators.pattern(this.nameRegex)]],
lastName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(128), Validators.pattern(this.nameRegex)]],
address: this.formBuilder.group({
country: ['', [Validators.required]],
city: ['', [Validators.required]]})})
The way I get the controls of the main form (registerForm) is by creating a simple return method:
get controls(){
return this.registerForm.controls;
}
And the way I get the controls of the nested form (address) is by creating another method:
get addressControls(){
return ((this.registerForm.get('address') as FormGroup).controls)
}
So if you need to do a *ngIf in the HTML for the main form use first method
*ngIf="controls.firstName.errors && ...
And for the nested form:
*ngIf="addressControls.country.errors && ...
I hope this might help!