Checking Multiple Row Custom Validator in Angular

2019-02-20 21:37发布

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

EXAMPLE

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;
  }

1条回答
相关推荐>>
2楼-- · 2019-02-20 22:14

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

查看更多
登录 后发表回答