In Angular 7.x using the formBuilder and ReactiveForms, I am trying to put a validator to a form which is based on the role the user has. So when the user has a different role, he/she is required to put in a field. I store this user in a variable stored in the class.
I don't want to put the validator on the subscription of valueChange but on the initial build. How am I able to do so? Below some code.
buildForm(): void {
this.accountForm = this.formBuilder.group({
firstName: [this.user.firstName, Validators.required],
initials: [this.user.initials, Validators.required],
lastNamePrefix: [this.user.lastNamePrefix],
lastName: [this.user.lastName, Validators.required],
cellPhoneNumber: [this.user.cellPhoneNumber], <-- make this one required if the role of the user is x
]),
});
}