ifValidator / ifAsyncValidator
Khaldor48 opened this issue · comments
Hello,
If I add some ifValidator
'foo': [
'',
ifValidator(() => this.areAdditionalFieldsRequired, [Validators.required]),
],
control.hasValidator(Validators.required)
Is always false
even If it's correctly applied
Should I check it somehow differently or would be please possible to add some feature to check .hasIfValidator ?
Of course I know I can discover required status from the property this.areAdditionalFieldsRequired
, but I have generic components for different form controls and there it is infered from
isRequired = signal(false);
this.isRequired.set(this.control.hasValidator(Validators.required))
So I could do that with additional Input property but it would be nice to infer it from hasValidator function directly
Thank you very much :)
In the current implementation, any validators provided are composed, which breaks these presence checks. I'm about to submit an improvement so that individual validators are preserved. Perhaps further changes can be made to preserve multiple ones by composing them in a different way (that could potentially be its own util 🙂)
It's worth noting that since hasValidator
checks by reference, validator factories passed directly will also return false
, as they do without this helper, unless the result is saved in a property.
const maxValidator = Validators.max(10);
const control1 = new FormControl(Validators.max(10));
const control2 = new FormControl(maxValidator));
control1.hasValidator(Validators.max(10)); // false
control1.hasValidator(maxValidator); // false
control2.hasValidator(Validators.max(10)); // false
control2.hasValidator(maxValidator); // true
(this is not the case here, just posting for clarification)