Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Created January 26, 2017 18:20
Show Gist options
  • Save VictorCoding/9bf9f4b8c0fe072000404543a473f1b9 to your computer and use it in GitHub Desktop.
Save VictorCoding/9bf9f4b8c0fe072000404543a473f1b9 to your computer and use it in GitHub Desktop.
angular2 form
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