I need help with formArray
validation in reactive form. I want validate each item in array, but i have no idea how can i do that. Thanks.
html code:
<label for="name">name:</label>
<input formControlName="name" id="name" type="text">
<div *ngIf="name.invalid && (name.dirty || name.touched)">
<div *ngIf="name.errors.required">
required
</div>
</div>
TypeScript code:
createForm() {
this.form = this.fb.group({
person: this.fb.array([this.initItemRows()])
});
}
initItemRows() {
return this.fb.group({
name: [''],
info: ['']
});
}
addNewRow() {
const control = <FormArray>this.form.controls['person'];
control.push(this.initItemRows());
}
deleteRow(index: number) {
const control = <FormArray>this.form.controls['person'];
control.removeAt(index);
}
get name() { return this.form.get('person.name'); }
get info() { return this.form.get('person.info'); }
I tried this:
initItemRows() {
return this.fb.group({
name: ['', Validators.required ],
info: ['', Validators.required ]
});
}
It doesnt work..