gpt4 book ai didi

angular - 响应式(Reactive)表单数组 - 推送新元素时避免验证错误

转载 作者:行者123 更新时间:2023-12-03 22:55:07 25 4
gpt4 key购买 nike

我有一个由单个表单数组组成的表单组:

ngOnInit() {
this.deviceDetailsFormGroup = this._formBuilder.group({
deviceDetails: this._formBuilder.array([
this.buildDeviceDetailsForm()
])
});
}
Form Array 中的每个控件都需要验证器:
buildDeviceDetailsForm(): FormGroup {
return this._formBuilder.group({
ipAddressCtrl: [
"",
[Validators.pattern(ipaddressPattern), Validators.required]
],
hostnameCtrl: [
"",
[
Validators.required,
Validators.maxLength(30),
Validators.minLength(5)
]
]
});
}
下面是我对 Form Array 的 push 和 remove 函数:
addNewDevice() {
this.deviceItems = this.deviceDetailsFormGroup.get(
"deviceDetails"
) as FormArray;

if (this.deviceItems.length > MAX_DEVICES) {
this.toastNotif.errorToastNotif(
`A maximum of ${MAX_DEVICES} devices can be selected for a single job scan`,
"Too many devices selected"
);
return;
}

if (this.deviceDetailsFormGroup.invalid) {
return;
}

let mapIP = new Map<string, number>();

// Warn about duplicate IP's in form
this.deviceItems.controls.forEach(ctrl => {
if (mapIP[ctrl.value.ipAddressCtrl] === 0) {
this.toastNotif.warningToastNotif(
"You have entered duplicate IP's in the form fields",
"Duplicate" + " IP's"
);
}

mapIP[ctrl.value.ipAddressCtrl] = 0;
});

this.deviceItems.push(this.buildDeviceDetailsForm());
}

removeDevice(i: number) {
this.deviceItems.removeAt(this.deviceItems.length - 1);
}
当我将新元素推送到 Form Array 时,它们被标记为无效,尽管它们未受影响且原始。我知道这是由创建新的 FormGroup 时设置的验证器和空默认值引起的。
是否可以避免这种行为,以便 FormArray 元素仅在被触摸时才被标记为错误?
提前致谢。

最佳答案

Luca,FormArray 中的formControl 会一直无效,所以FormArray 无效,表单无效。您需要检查是否触摸了控件。 this stackblitz 中的一个简单示例。你看你加的话,控件无效,但是没碰过

.html 是一种典型的形式

<button (click)="add()">Add</button>
<form [formGroup]="myForm">
<div formArrayName="myFormArray">
<div *ngFor="let group of myFormArray.controls;let i=index" [formGroupName]="i">
<input formControlName="name">
<!--show only after touched and invalid-->
<div *ngIf="group.get('name').touched && group.get('name').invalid">
invalid and touched
</div>
<!--show always when add a new formGroup to array-->
<div *ngIf="group.get('name').invalid">
invalid
</div>
</div>
</div>
</form>

.ts 是“经典”
myForm=new FormGroup({
myFormArray:new FormArray([])
})

get myFormArray()
{
return this.myForm.get('myFormArray') as FormArray
}
createGroup()
{
return new FormGroup({
name:new FormControl('',Validators.required)
})
}
add()
{
this.myFormArray.push(this.createGroup())
}

关于angular - 响应式(Reactive)表单数组 - 推送新元素时避免验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514431/

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