How should I go about conditionally requiring a form field? I made a custom validator, but the conditional variables that I pass to the custom validator are static and remai
I created a validator that accepts a function callback which allows me to make the validator reusable, but I can't seem to find a good way to make sure I call updateValueAndValidity() on the control with this validator without having to manually call it when the other control's value changes.
Validator
export class ValidationService {
static conditionalRequired = (isRequiredFunction: Function) => {
return (control: Control) => {
if (!control.value && isRequiredFunction())
...
Component Page
private myControl1: Control =
new Control("", ValidationService.conditionalRequired(() =>
{ return this && this.model && this.model.SomeProperty === 'SomeValue' } ));
private myControl2: Control =
new Control("",
ValidationService.conditionalRequired(this.isControl2Required.bind(this)));
isControl2Required() {
return someCondition;
}