1

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.

1 Answer 1

1

try this

this.rForm.controls['subCategoryName'].clearValidators();

or you can use setvalidators and set it to null

this.rForm.controls['subCategoryName'].setValidators(null);
Sign up to request clarification or add additional context in comments.

3 Comments

No. It didnt make any difference. Same result
is required validation is triggered before removing the validators
if validation is triggerred it wont remove it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.