Last active
June 21, 2019 21:12
-
-
Save sheniff/0868b1a042c201eaee38b86da45d3e58 to your computer and use it in GitHub Desktop.
conditional validator for Angular reactive forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const requiredIf(condition: (control: AbstractControl) => boolean): (control: FormControl) => ValidationResult { | |
return (control: FormControl): ValidationResult => { | |
// Important: control.parent might not be defined, depending on your form structure | |
return !condition(control.parent) || !!control.value ? null : { [`required_if`]: true }; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment