我有一个反应式的自定义验证器,用于验证字段值不重叠。 我不需要将整个组中的每个字段都显示为错误,而是需要将触发字段状态设置为无效。
groupValidation(c: AbstractControl): {[key: string]: string} | null {
let compareToVariables = [{
id:"t1",
name:"Test 1",
value: c.get('t1').value
}, {
id:"t2",
name:"Test 2",
value: c.get('t2').value
}, {
id:"t3",
name:"Test 3",
value: c.get('t3').value
}]
for (var x = 0; x < compareToVariables.length; x++) {
var compareToVariable = compareToVariables[x];
for (var y = 0; y < compareToVariables.length; y++) {
var valueToCompare = compareToVariables[y];
if(valueToCompare.value === compareToVariable.value) {
//here I want to set the status of the field that was triggered the error to 'invalid' but it doesn't work (undefined is not an object)
this.datainputForm.setError(valueToCompare.id: 'invalid')
let errorMessageText = 'Field' + valueToCompare.name + 'overlaps with ' + compareToVariable.name
return {'overlap': errorMessageText);
}
}
}
}
我不知道为什么找不到像我调用的其他函数中那样的形式(例如,用于更改其他字段中的值的valueChange侦听器)。
谢谢大家!
#####编辑###### 这是此问题的快速解答:https://stackblitz.com/edit/clarity-light-theme-v012-ia5zpn
如您所见,只有两个重叠时,所有输入字段都会突出显示,因此在groupValidation()
方法中,我只想设置触发条件(或两个重叠的字段),以便将html设置为{ {1}}或... || datainputForm.get('testGroup.t1').errors"
或... || datainputForm.get('testGroup.t2').errors"