Checking Multiple Row Custom Validator in Angular 4 Reactive Forms

[亡魂溺海] 提交于 2019-12-30 14:48:30

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/48614017/checking-multiple-row-custom-validator-in-angular-4-reactive-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!