gpt4 book ai didi

javascript - 检查 Angular 响应式(Reactive)表单中的 FormGroups 字段中是否存在重复项

转载 作者:行者123 更新时间:2023-12-02 01:52:49 25 4
gpt4 key购买 nike

在保存所有网格行时,我需要检查特定列字段是否有重复项

 (this.formGroups.get('items') as FormArray).controls.forEach((item) => {
console.log(item.value.attributeDisplayName);
});

attributeDisplayName 存在重复值时,我需要通过显示警报或类似内容来阻止表单提交。我如何在当前的 forEach 循环中检查这一点。谢谢

我在 HTML 标记中使用 kendo Angular 组件,但没有使用表单标签。下面是列声明的示例

  <kendo-grid-column field="attributeDisplayName" title="CUSTOM FIELD LABEL" width="190">  
<ng-template kendoGridEditTemplate let-column="column" let-formGroup="formGroup" >
<input ngDefaultControl class="k-textbox" [formControl]="formGroup.get(column.field)">

<div *ngIf="formGroup.get(column.field).errors && (formGroup.get(column.field).dirty || formGroup.get(column.field).touched)">
<span style="color:red" class="k-icon k-i-warning"></span>
<span style="color:red">CUSTOM FIELD LABEL is a required field</span>

</div>

</ng-template>
</kendo-grid-column>

下面是 .ts 文件中的 FormGroup 声明

    public formGroup: FormGroup;
public formGroups: FormGroup = new FormGroup({ items: new FormArray([]) });

public createFormGroup = (dataItem) =>
new FormGroup({
atrributeId: new FormControl(dataItem.atrributeId),
objectType: new FormControl(dataItem.objectType, Validators.required),
attributeDisplayName: new FormControl(dataItem.attributeDisplayName, Validators.required),
dataType: new FormControl(dataItem.dataType, Validators.required),
inputValues: new FormControl(dataItem.inputValues, [Validators.required, InputValuesValidator]),
isGridEligible: new FormControl(dataItem.isGridEligible, Validators.required),
isInvoiceEligible: new FormControl(dataItem.isInvoiceEligible, Validators.required),
});

我使用此 FormGroup 的示例函数

    public editRows(grid) {      
this.isEdited = true;
let currentRow = 0;
let rows: any = grid.data.data;

for (let i = 0; i < rows.length; i++) {
const formGroup = this.createFormGroup(rows[i]);
this.formGroup = formGroup;
(this.formGroups.get('items') as FormArray).push(formGroup);
grid.editRow(currentRow, formGroup, {skipFocus: true});
currentRow++;
}
}

最佳答案

不必在提交时检查它,只需在填写表单时检查它,如果用户填写了重复的值,则立即显示错误。

Working demo here.

使用此函数来检查重复项 -

checkDuplicacy(event, field: FormControl) {
let length = this.formGroups.value.items.length;
let count = 0;
let controls = (<FormArray>this.formGroups.controls.items).controls;
for (let i = 0; i < length; i++) {
if (
this.formGroups.value.items[i].attributeDisplayName.toLowerCase() ==
event.target.value.toLowerCase()
) {
count++;
}
if (count > 1) {
field.markAsTouched();
field.setValidators((f) => <any>{ duplicateName: true });
field.updateValueAndValidity();
} else {
field.clearValidators();
field.setValidators([Validators.required]);
field.updateValueAndValidity();
}
}
}

并更新了 HTML -

    <kendo-grid-column field="attributeDisplayName" title="CUSTOM FIELD LABEL" width="190">  
<ng-template kendoGridEditTemplate let-column="column" let-formGroup="formGroup" >
<input ngDefaultControl class="k-textbox" [formControl]="formGroup.get(column.field)" (blur)="checkDuplicacy($event, formGroup.get(column.field))">


<div *ngIf="formGroup.get(column.field).errors && (formGroup.get(column.field).dirty || formGroup.get(column.field).touched)">
<span style="color:red" class="k-icon k-i-warning"></span>
<span style="color:red">CUSTOM FIELD LABEL is a required field</span>

</div>

<span *ngIf="formGroup.get(column.field).errors?.duplicateName" style="color:red">
Duplicate field.</span>

</ng-template>
</kendo-grid-column>

关于javascript - 检查 Angular 响应式(Reactive)表单中的 FormGroups 字段中是否存在重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70007539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com