问题
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