I'm using editable with formarray. My model:
class Book {
id: number;
name: string;
active: boolean;
}
allBooks:
[
{id: 1, name: 'book1', active: true},
{id: 2, name: 'book2', active: true},
{id: 3, name: 'book3', active: true},
]
code snippet:
allBooks: Book[];
bookFg: FormGroup;
ngOnInit() {
this.bookFg = this.fb.group({
arrayForm: this.fb.array(allBooks.map(book => {
id: [book.id],
name: [book.name],
active: [book.active]
}))
});
}
I have to validate the book name, name is required and unique. The html snippet:
<div class="data-container" [formGroup]="bookFg">
<p-table id="resultTable" [columns]="cols" [value]="labelForm.get('arrayForm').controls" formArrayName="arrayForm" dataKey="value.id" scrollable="true" [resizableColumns]="true" scrollHeight="415px" selectionMode="single"
[selection]="selected" (onRowSelect)="onRowSelect($event.data)">
<ng-template pTemplate="header" let-columns>
...
...
...
</ng-template>
<ng-template pTemplate="body" let-rowData let-rowIndex="rowIndex">
<tr [pSelectableRow]="rowData" [formGroupName]="rowIndex">
<td>
<div class="text-center">
<input pInputText type="checkbox" formControlName="active">
</div>
</td>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<input (focus)="onFocusEvent(rowIndex)" (blur)="onBlurEvent()" pInputText type="text" formControlName="name">
</ng-template>
<ng-template pTemplate="output">
{{rowData.get('name').value}}
</ng-template>
</p-cellEditor>
</td>
</tr>
</ng-template>
</p-table>
</div>
In this editable table, each row is formgroup. When after editing the name columns, this row will be saved. Question is how to validate? In my case, only save one row in one time. So should I validate all the formarray or just one formgroup in that formarray? and how?