简体   繁体   中英

Angular 4 Form Control Removing Validation/Errors

I have a form control that I am dynamically setting validators and removing validators. Which is fine, however -- all my form controls are marked as status invalid when I try to remove and set.

My code for removing:

this.answerControlArr[pvqQuestionId - 1].clearValidators();
this.answerControlArr[pvqQuestionId - 1].updateValueAndValidity();

My code for setting:

this.answerControlArr[pvqQuestionId - 1].setValidators(Validators.compose([Validators.required, Validators.minLength(4)]));
this.answerControlArr[pvqQuestionId - 1].updateValueAndValidity();

When I print to the console, I see in the browser that the errors are still there and the status is marked as invalid? I have also tried using before setting validators:

this.answerControlArr[pvqQuestionId - 1].reset();

在此处输入图片说明

Even though this is old question, I'm answering this as it may help to someone.I also tried three option you have used. Try below code as well with existing ones.

this.answerControlArr[pvqQuestionId - 1].reset();
this.answerControlArr[pvqQuestionId - 1].clearValidators();
this.answerControlArr[pvqQuestionId - 1].updateValueAndValidity();
this.answerControlArr[pvqQuestionId - 1].markAsPristine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM