Skip to content

Instantly share code, notes, and snippets.

@sheniff
Last active June 21, 2019 21:12
Show Gist options
  • Save sheniff/0868b1a042c201eaee38b86da45d3e58 to your computer and use it in GitHub Desktop.
Save sheniff/0868b1a042c201eaee38b86da45d3e58 to your computer and use it in GitHub Desktop.
conditional validator for Angular reactive forms
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