Reputation: 513
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.
Upvotes: 1
Views: 1162
Reputation: 6311
try this
this.rForm.controls['subCategoryName'].clearValidators();
or you can use setvalidators and set it to null
this.rForm.controls['subCategoryName'].setValidators(null);
Upvotes: 1