angularangular-formsangular-validation

Angular - Dynamically add/remove validators


I have a FormGroup defined like below:

this.businessFormGroup: this.fb.group({
    'businessType': ['', Validators.required],
    'description': ['', Validators.compose([Validators.required, Validators.maxLength(200)])],
    'income': ['']
  })

Now when businessType is Other , I want to remove Validators.required validator from description. And if businessType is not Other, I want to add back the Validators.required.

I am using the below code to dynamically add/remove the Validators.required. However, it clears the existing Validators.maxLength validator.

if(this.businessFormGroup.get('businessType').value !== 'Other'){
    this.businessFormGroup.get('description').validator = <any>Validators.compose([Validators.required]);               
} else {                
    this.businessFormGroup.get('description').clearValidators();               
}

this.businessFormGroup.get('description').updateValueAndValidity(); 

My question is, how can I retain the existing validators when adding/removing the required validator.


Solution

  • If you are using Angular 12.2 or higher, you can use the AbstractControl methods addValidators, removeValidators, and hasValidator, as per the docs:

    if(this.businessFormGroup.get('businessType').value !== 'Other'){
        this.businessFormGroup.get('description').addValidators(Validators.required);               
    } else {                
        this.businessFormGroup.get('description').clearValidators();               
    }
    

    For older versions, Angular forms have a built in function setValidators() that enables programmatic assignment of Validators. However, this will overwrite your validators.

    For your example you can do:

    if(this.businessFormGroup.get('businessType').value !== 'Other'){
        this.businessFormGroup.controls['description'].setValidators([Validators.required, Validators.maxLength(200)]);              
    } else {                
        this.businessFormGroup.controls['description'].setValidators([Validators.maxLength(200)]);               
    }
    this.businessFormGroup.controls['description'].updateValueAndValidity();
    

    It is important to keep in mind that by using this method you will overwrite your existing validators so you will need to include all the validators you need/want for the control that you are resetting.