Luke LaFountaine
Luke LaFountaine

Reputation: 848

How to validate Angular2 form controls that depend on eachother?

I am trying to create a time picker component that binds to a reactive form control. I want the form control to be invalid if the end time is before the start time. I am aware of the built in validators that Angular comes with, but I want to be able to validate against another form control. If someone could point me in the right direction that would be great.

Upvotes: 3

Views: 873

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97381

This is the approach I take:

  • Define the validator on the FormGroup containing the two controls.
  • In the validator, use the FormGroup object that is passed in to retrieve the values of the two FormControl objects and perform the validation.

Upvotes: 3

Related Questions