I have form Array , which controls has to be required , based on a variable ,now I wrote custom validator like below
recurringValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (!control.value) {
return null;
}
if (this.myVariable === 'constant1') {
return Validators.required;
}
return null;
};
}
now this will check the value of the 'myVariable' and put required but somehow its not working , I am adding this custom validators like below
const fbGroup = this.fb.group({
recurringControl1: this.fb.control('', [this.recurringValidator()]),
recurringControl2: this.fb.control('', [this.recurringValidator()]),
recurringControl3: this.fb.control('', [this.recurringValidator()])
});
this.myForm = this.fb.group({
control1: fb.control('', [this.recurringValidator()]),
groupcontrol1: fb.array([])
});
when I click on submit , form is valid when even myVariable value is 'constant1' .
any suggestion or modifications please .
here is the stackblitz link https://stackblitz.com/edit/angular-form-array-validators?
Validators.requredfrom async validator function ? @Shaswata