I've got the template under *ngIf and it only generates after the form is loaded. Additionally, I've got the failing form under a dynamically displayed field, which depends on a button press. I log out the form, and it returns the FormControl which is missing correctly.
Template:
<div *ngIf="ready">
<form [formGroup]="form">
<div [formGroup]="form.controls.request">
<div *ngFor="let field of inputFields">
...
</div>
</div>
<div *ngIf="somethingEnabled">
<div [formGroup]="form.controls.Something">
<div class="input-container">
<label>Name: </label>
<input formControlName="Name">
</div>
<div [formGroup]="form.controls.Something.Organization">
<div class="input-container">
<label>ASD: </label>
<input formControlName="ASD">
</div>
<div [formGroup]="form.controls.Something.Organization.Other">
<div class="input-container">
<label>Id: </label>
<input formControlName="Id">
</div>
<div [formGroup]="form.controls.Something.Organization.Other.SchmeNm">
<div class="input-container">
<label>SchmeName: </label>
<input formControlName="SchmeName">
</div>
<div class="input-container">
<label>SchmeValue: </label>
<input formControlName="SchmeValue">
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
code:
generateForm() {
this.form = this.fb.group({
request: this.fb.group(
this.generateRequestFields())
,
Something: this.fb.group(this.generateSomething()),
address: this.fb.group(
this.generateAddress()
)
});
}
generateSomething() {
const returnObject = {};
returnObject['Name'] = new FormControl();
returnObject['Organization'] = this.fb.group(this.generateOrganization());
return returnObject;
}
generateOrganization() {
const returnObject = {};
returnObject['ASD'] = new FormControl();
returnObject['Other'] = this.fb.group({
Id: new FormControl(),
Schme: this.fb.group({
SchmeName: new FormControl(),
ShcmeValue: new FormControl()
}),
Issr: new FormControl()
});
return returnObject;
}
I only get the error when activating the form.controls.something part. It works fine if I remove form.controls.Something.Organization part of the template.
Error: ERROR Error: formGroup expects a FormGroup instance. Please pass one in.
I might have just missed something, and was hoping I'll find it while posting this, but still can't seem to get it