在Angular 4反应形式中检查多行自定义验证器 [英] Checking Multiple Row Custom Validator in Angular 4 Reactive Forms

查看:107
本文介绍了在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

未实现ControlValueAccessor的示例

这篇关于在Angular 4反应形式中检查多行自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆