I have a form like below
this.personForm = new FormGroup({
title: new FormControl(this.person.salutation, Validators.compose([
Validators.minLength(2)
])),
firstName: new FormControl(this.person.firstName, Validators.compose([
Validators.minLength(4)
])),
});
Based on a configuration from an API call I want to make them dynamically required or not. I know I can use formControl.setValidators([Validators.required]) but that will clear existing Validators.
I was hoping to achieve it in one of the following ways
- A function for just adding (Exp:
addValidators([])) - OR a function that would give me the list of Validators a formControl already has, so that I can join them with what I want to add. (Exp:
getValidators())
Unfortunately(as for as I know) none of above 2 functions exist. So how can one add a new Validation to a formControl without having to know already existing ones?
titleafter form is inited but without losing old Validations (In this case Validator.minLength(2))?. Thanks - Herbi Shtini