在Angular 4反应形式中检查多行自定义验证器
我有一个非常简单的问题,我无法解决.首先,我有一个批准数量的成分.该成分有多个有效期.但是,我要检查我的转让数量"到期日是否不超过批准数量.
I have a very simple problem which I can't get it. First, I have an ingredient which has an approved quantity. And that ingredient has several expiration dates. However, I want to check if my expiration dates which has the "quantity to transfer" is not more than the approved quantity.
我应该如何检查?我已经完成了一些我无法比较的零件,因为它必须对照一个批准的数量检查几行要转移的数量".这是下面的代码链接:
How should I check this? I already finished some parts BUT this one I couldn't compare since it has to check several rows of "quantity to transfer" against the one approved quantity. Here's the code link below:
customValidator(group: any) {
if ((group.controls.transfer_qty.value > group.parent.parent.controls.approved_qty.value)) {
return { out1: true }
}
if ((group.controls.transfer_qty.value > group.controls.available_qty.value)) {
return { out2: true }
}
return null;
}
当涉及嵌套表单时,我通常实现自定义表单控件. 一个好的规则是将代码抽象为小块,在我们的案例中,将有角度的代码分解为小组件.
I usually implement custom form controls when nested forms are involved. A good rule is to abstract out code into small pieces, in our case break out your angular code into small components.
您可以使用 ControlValueAccessor
我修改了您的示例以显示我的意思: 修改示例
I modified your example to show what I mean: Modified Example