6

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.

Karthikeyan Mohan
  • 319
  • 2
  • 4
  • 11
  • generally the errors are showed only if the control is invalid and touched, so the idea is markAsUntouched the FormControls: https://angular.io/api/forms/AbstractControl#markasuntouched (well you need loop over all the FormControls in your FormGroup) – Eliseo Jul 07 '21 at 21:06

2 Answers2

2

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
Hary
  • 5,690
  • 7
  • 42
  • 79
-1

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

Rahul Jujarey
  • 199
  • 1
  • 4