我有一个FormArray在FormGroup并且每个FormArray的有多个FormGroup的和可以动态地添加他们。
我有一个自定义验证,它检查与每个FormArray的所有数据,以验证数据的重复。 目前,它也验证自身被抛出一个错误的初始数据。
有没有办法从自身抛出时,它的校验初始数据限制错误?
它的工作很好,当添加新的数据,并具有与现有的相同的值。
for (let assign of this.additionalAssign) {
const fg = new FormGroup({
"payRate": new FormControl(assign.jobRate, Validators.required),
"position": new FormControl(assign.position, Validators.required),
"location": new FormControl(assign.location, Validators.required),
"department": new FormControl(assign.department, Validators.required)
});
fg.validator = this.jobDataValidator.bind(this);
this.addPay.push(fg);
}
验证器:
jobDataValidator(control: FormControl): {[s: string]: boolean} {
let value = this.getJobLocDeptValidity(control);
if(value.length > 0){
return {'sameJobData': true};
}
return null;
}
getJobLocDeptValidity(control: FormControl):any[] {
let additionalAssignments = this.additionalAssignmentsForm.value.payArray;
let test = additionalAssignments.filter(item => !!control.value.position && item.position === !control.value.location && item.location === control.value.location);
return test;
}
截图:
Stackblitz网址: https://stackblitz.com/edit/angular-custom-validator-defaultdata