Home > Software engineering >  Angular Cross-field validation on Dynamic Form
Angular Cross-field validation on Dynamic Form

Time:12-08

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/app.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.

CodePudding user response:

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
}
  • Related