I have the below form group.
this.rForm = fb.group({
'categoryName': [null, Validators.required],
'categoryImage': [null, Validators.required],
'mainCategoryId': [null, Validators.required],
'subCategoryName': [null, Validators.required]
});
And "mainCategoryId" refers to a dropdownlist. When i change value in that drowpdown, according to the value, i want to keep or remove the validators of "subCategoryName". Then i use below code in ngOnInit().
this.rForm.get('mainCategoryId').valueChanges.subscribe(
(mainCategoryId) => {
if(!this.isSub){
this.rForm.get('subCategoryName').clearValidators();
this.rForm.get('subCategoryName').updateValueAndValidity;
}
}
);
It comes inside the IF statement, but it is not removing the validators. Still checks for required. What am i doing wrong? I simply want to remove the validation for required.