Created
January 26, 2017 18:20
-
-
Save VictorCoding/9bf9f4b8c0fe072000404543a473f1b9 to your computer and use it in GitHub Desktop.
angular2 form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fields = [ | |
{ | |
label: 'First Name', | |
name: 'firstName', | |
required: true, | |
maxLength: 55 | |
}, | |
{ | |
label: 'Last Name', | |
name: 'lastName', | |
required: true, | |
maxLength: 55, | |
}, | |
{ | |
label: 'Phone', | |
name: 'phone', | |
required: false, | |
maxLength: 10, | |
} | |
] | |
let group: any = {}; | |
fields.forEach(field => { | |
group[field.code] = field.required ? new FormControl('', Validators.required) | |
: new FormControl(''); | |
}); | |
const registrationGroup = new FormGroup(group); | |
<form [formGroup]="registrationGroup"> | |
<input *ngFor="let field of fields" [formControlName]="field.name"> | |
<div *ngIf="!registrationGroup.get(field.name).valid"> | |
Field not valid. | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment