Skip to content

Instantly share code, notes, and snippets.

View sheniff's full-sized avatar

Sergio Almécija sheniff

  • Dropbox Inc
  • San Francisco
View GitHub Profile
@sheniff
sheniff / requiredIf.ts
Last active June 21, 2019 21:12
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 };
};
}