I am new to angular and recently working on creating a dynamic form with validations from response given by api. The api will provide the form field like Date type input type or multi select. Also the api response will provide the validations like field is required or not and regex for the input field if exist.
Using all the above data I need to create a form in angular which should be working like a normal form with validations like touched, untouched, error on reuqired field, error on validators etc.
Public URL of the page created using html and javascript. The api will be available from network tabs
https://devemr.growthemr.com/assets/static/form.html?bid=1413&fid=9242
Below is the code base I tried.
form.component.ts
ngOnInit(): void {
this.loadQuestions(); // it is the API call and I have attached the sample API response
this.form = this.fb.group({
items: this.fb.array([])
})
}
addItems() {
this.contactFormQuestions.forEach((x: any) => {
this.itemsFormArray.push(this.fb.group({
[x.id]: ['', x.required ?[ Validators.required] : null, x.regex? [Validators.pattern(x.regex)]: ''],
}));
});
console.log(this.form.get('items'));
}
get itemsFormArray() {
return this.form.get('items') as FormArray;
}
form.component.html
<form [formGroup]="form">
<div formArrayName="items">
<div *ngFor="let pqa of itemsFormArray.controls; let i = index">
<div [formGroupName]="i">
<div [ngSwitch]="pqa.value.questionType" class="switchCase">
<div *ngSwitchCase="'Input'" class="input-feild">
<input type="text" class="form-control" [formControlName]="pqa.value.id">
</div>
<div *ngSwitchCase="'Date'" class="input-feild">
<input type="date" class="form-control" [formControlName]="pqa.value.id">
</div>
</div>
</div>
</div>
</div>
</form>
API response
[
{
"id": 59233,
"questionId": 74719,
"questionName": "fname",
"questionType": "Input",
"hidden": null,
"required": true,
"validate": null,
"regex": "[A-Za-z]",
"validationMessage": null,
"answer": false,
"questionChoices": null,
"patientQuestionChoices": [],
"allowMultipleSelection": false,
"showDropDown": null,
"preSelectCheckbox": false
},
{
"id": 59234,
"questionId": 74720,
"questionName": "Date",
"questionType": "Date",
"hidden": null,
"required": true,
"validate": null,
"regex": null,
"validationMessage": null,
"answer": false,
"questionChoices": null,
"patientQuestionChoices": [],
"allowMultipleSelection": false,
"showDropDown": null,
"preSelectCheckbox": false
},
{
"id": 59235,
"questionId": 74721,
"questionName": "Multi Select",
"questionType": "Multiple_Selection_Text",
"hidden": null,
"required": true,
"validate": null,
"regex": null,
"validationMessage": null,
"answer": false,
"questionChoices": null,
"patientQuestionChoices": [
{
"deleted": false,
"tenantId": 1413,
"id": 3993,
"choiceName": "Option1",
"choiceId": 8546,
"selected": false
},
{
"deleted": false,
"tenantId": 1413,
"id": 3994,
"choiceName": "Option2",
"choiceId": 8547,
"selected": false
}
],
"allowMultipleSelection": true,
"showDropDown": true,
"preSelectCheckbox": false
}
]
: anyto cast all you types to something untyped. you will lose a lot of help from typescript doing that.