7

How to clear the errors on form control. I have a method that tries to clear the errors on the form control but in vain.

this.form.controls[ 'postalCode' ].setErrors(null);

The form control name is postalCode and when I set the error to null, it doesn't remove the error from that control.

1
  • generally the errors are showed only if the control is invalid and touched, so the idea is markAsUntouched the FormControls: angular.io/api/forms/AbstractControl#markasuntouched (well you need loop over all the FormControls in your FormGroup) Commented Jul 7, 2021 at 21:06

2 Answers 2

3

To clear all the errors

this.form.get('postalCode').setErrors(null);

To clear by its key

this.form.get('postalCode').setErrors({key: null});
this.form.get('postalCode').updateValueAndValidity(); //may need this in child components
Sign up to request clarification or add additional context in comments.

Comments

-1

try doing this.form.controls[ 'postalCode' ].setErrors({});

Comments

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.