Angular Cross-field validation on Dynamic Form

224 Views Asked by At

I built a dynamic reactive form that reads a json file (or just json in general) and renders the form elemtns for the user here.

https://stackblitz.com/edit/angular-empty-project-qzjttt?file=app%2Fapp.component.ts

... I've been trying to figure out the best way to validate the three dates in the form where the first date is less than the second date and the second is less than the third date. After doing some research I figured a cross-field validation might be the way to go. But I'm not sure how to write one, when you're getting the names of the form control from the json.

1

There are 1 best solutions below

0
On BEST ANSWER

You are going the right direction. What you could do is make your validator function receive the control name instead. And then you can use control.parent to get the formgroup itself and also checks its value.

if(control.parent && control.parent.get(OTHERKEY).value) {
    // check here if it is less then the current value
}