我正在使用带有formarray的可编辑。我的模型:
class Book {
id: number;
name: string;
active: boolean;
}
所有书籍:
[
{id: 1, name: 'book1', active: true},
{id: 2, name: 'book2', active: true},
{id: 3, name: 'book3', active: true},
]
代码片段:
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]
}))
});
}
我必须验证书名,名称是必需的并且是唯一的。html片段:
<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>
在这个可编辑的表格中,每一行都是 formgroup。编辑名称列后,将保存该行。问题是如何验证?就我而言,一次只能保存一行。那么我应该验证所有表单数组还是只验证该表单数组中的一个表单组?如何?