Reputation: 720
I don't see a good way to reuse validators of underlying controls. For example, I have a custom control which wraps up material datepicker which has four validators: min, max, filter and parse. Custom control could extend datepicker's functionality by adding additional buttons, timepicker or something else but it's clear that it would be nice to use original validators instead of duplication them.
Does someone know a good approach?
Thanks in advance!
Upvotes: 2
Views: 65
Reputation: 442
I'm really not sure what you wan to do exactly but validators are in the end just functions. If for example you want to dynamically add a validator to your existing control all while keeping old controls you can do something like this:
const fieldControl = myForm.get(fieldPath); // see FormGroup.get(path)
// adding new validator while keeping old ones with fieldControl.validator since setValidators() resets validators
fieldControl.setValidators([validator, fieldControl.validator]);
fieldControl.updateValueAndValidity();
This means that you can get all your validators with fieldControl.validator and do whatever you want with it.
Upvotes: 1