Skip to content

Instantly share code, notes, and snippets.

@camilogiraldo
Created February 28, 2019 01:51
Show Gist options
  • Save camilogiraldo/948028988c0c09e0309900f6662651f7 to your computer and use it in GitHub Desktop.
Save camilogiraldo/948028988c0c09e0309900f6662651f7 to your computer and use it in GitHub Desktop.
<form [formGroup]="form" (submit)="submitForm()">
<ng-template ngFor let-input [ngForOf]="formData">
<ng-container [ngSwitch]="input.controlType">
<!-- handling text type inputs -->
<ng-template [ngSwitchCase]="'text'">
<div class="form-group">
<label [for]="input.controlName"> {{input.controlName}}</label>
<input [formControlName]="input.controlName" [type]="input.valueType" [name]="input.controlName"
[required]="input.validators.required" [minlength]="input.validators.minlength"
[maxlength]="input.validators.maxlength" />
<div class="error" *ngIf="form.get(input.controlName).invalid &&
(form.get(input.controlName).dirty || form.get(input.controlName).touched)">
<div *ngIf="form.get(input.controlName).errors.required">Input required</div>
<div *ngIf="form.get(input.controlName).errors.minlength">Minimum length is {{input.validators.minlength}}</div>
</div>
</div>
</ng-template>
</ng-container>
</ng-template>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment