@rapropos, thanks for reply. I made changes as per your suggestion as below:-
//Custom form validation
this.pricePerHour = false/true; //based on visible or not
this.bidForm = this.formBuilder.group({
Description: new FormControl('', Validators.required),
PerHour: new FormControl('', Validators.compose([this.PriceValidator(this.pricePerHour)]))
//this.pricePerHour is a boolean variable, initially false
});
Here I want to validate PerHour only if pricePerHour is true.
In this case form is checking PerHour field either pricePerHour is false or true (visible/disable).
Means boolean variable pricePerHour is always false inside priceValidator().
I also check with below but did not work:-
this.myForm = this.formBuilder.group({
Description: new FormControl('', Validators.required),
PerHour: new FormControl('', Validators.compose([this.pricePerHour ==
true ? null : Validators.required]))
});
The way you’ve written this, the value of pricePerHour is locked in at the time that priceValidator is called. So you’re fighting the flow a bit. What I would suggest is adding/removing the validator at the time pricePerHour changes.