<form [formGroup]="dynamicForm" (ngSubmit)="onSubmit()" novalidate>
<ng-container *ngFor="let controls of fields">
{{controls.label}}
<input [type]="controls.type" [formControlName]="controls.label"/>
<ng-container *ngFor="let validation of controls.validations">
<span *ngIf="dynamicForm.controls[controls.label].touched && dynamicForm.controls[controls.label].errors">
<p *ngIf="dynamicForm.controls[controls.label].errors[validation.name]">
{{validation.message}}
</p>
</span>
</ng-container>
<br>
</ng-container>
<br>
<input type="submit" [disabled]="!dynamicForm.valid"/>
</form>
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
fields = [
{
type: 'input',
label: 'Username',
inputType: 'text',
name: 'name',
validations: [
{
name: 'required',
validator: 'required',
message: 'Name Required'
},
{
name: 'pattern',
validator: '^[a-zA-Z]+$',
message: 'Accept only text'
}
]
}, {
type: 'password',
label: 'Password',
inputType: 'text',
name: 'name',
validations: [
{
name: 'required',
validator: 'required',
message: 'Password Required'
}
]
}
];
dynamicForm: FormGroup;
constructor() {
const controls = {};
this.fields.forEach(res => {
const validationsArray = [];
res.validations.forEach(val => {
if (val.name === 'required') {
validationsArray.push(
Validators.required
);
}
if (val.name === 'pattern') {
validationsArray.push(
Validators.pattern(val.validator)
);
}
});
controls[res.label] = new FormControl('', validationsArray);
});
this.dynamicForm = new FormGroup(
controls
);
}
onSubmit() {
console.log(this.dynamicForm.value);
}
}