Hi,
I have to load a json to generate form dynamically. It generates an error on first load.
Cannot find control with name:
There is a dropdown, which triggers a reload of data for form generation. The subsequent form generations work fine. Can someone help in identifying the error of first load please?
Code snippets of TS, view and json are as under.
JS/TS
updateForm(v){
JSON.parse(v.data.formhtml).formhtml.forEach(e=>{
e.field.forEach(ei=>{
this.formData[ei.name] = [''];
if(ei.validation == 'required')
this.formData[ei.name].push(Validators.required);
});
});
this.consform = this.formBuilder.group(this.formData);
this.formfields = JSON.parse(v.data.formhtml).formhtml;
}
Pages
form *ngIf="consform" [formGroup]="consform" (ngSubmit)="createItem()">
<label class="item item-input item-select">
<div class="input-label">
<select formControlName="type"
[(ngModel)]="selectedForm"
(ngModelChange)="updateForm($event)">
<option *ngFor="let form of loadedForms; let formIndex = index " [ngValue]="form"
> {{form.formname}}</option>
</select>
</div>
</label>
<hr>
<span *ngFor="let fm of formfields; let fi=index">
{{fm.title}}
<ion-list>
<ion-item *ngFor="let el of fm.field">
<span>{{el.label}}</span><ion-input type='text' placeholder="{{el.label}}" [formControlName]=el.name></ion-input>
</ion-item>
</ion-list>
</span>
</form>
JSON
{
"formhtml": [{
"title": "From",
"field": [{
"label":"Name",
"name": "fname",
"type": "text",
"validation": "required"
},
{
"label":"Port",
"name": "fportname",
"type": "text",
"validation": "required"
}]
}]
}
global packages:
@ionic/cli-utils : 1.4.0
Cordova CLI : 7.0.1
Ionic CLI : 3.4.0
local packages:
@ionic/app-scripts : 1.3.12
@ionic/cli-plugin-cordova : 1.4.0
@ionic/cli-plugin-ionic-angular : 1.3.1
Cordova Platforms : none
Ionic Framework : ionic-angular 3.5.0
System:
Node : v6.11.0
OS : Windows 8.1
Xcode : not installed
ios-deploy : not installed
ios-sim : not installed
npm : 5.0.4
Thanks.