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

查看:33
本文介绍了在 Angular 4 反应式表单中检查多行自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,我无法解决.首先,我有一种成分有批准的数量.而且这种成分有几个有效期.但是,我想检查我的转移数量"的到期日期是否不超过批准数量.

我应该如何检查这个?我已经完成了一些部分,但这个我无法比较,因为它必须根据一个批准的数量检查几行要转移的数量".代码链接如下:

customValidator(group: any) {如果((group.controls.transfer_qty.value > group.parent.parent.controls.approved_qty.value)){返回 { out1: true }}如果 ((group.controls.transfer_qty.value > group.controls.available_qty.value)) {返回 { out2: true }}返回空;}

解决方案

当涉及嵌套表单时,我通常会实现自定义表单控件.一个好的规则是将代码抽象成小块,在我们的例子中,将你的 angular 代码分解成小组件.

您可以使用 ControlValueAccessor

实现自定义表单控件

我修改了你的例子来说明我的意思:修改示例

未实现 ControlValueAccessor 的示例

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:

FORKED LINK HERE

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.

You can implement custom form controls using ControlValueAccessor

I modified your example to show what I mean: Modified Example

Example without implementing ControlValueAccessor

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

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