Angular Abstract control remove error

Much has already been stated in other posts to this question (e.g. delete the error property (which still leaves the control flagged as invalid), issues with updateValueAndValidity() in changeHandlers (infinite loop) etc). Putting it all together I now use the below small function, which leaves other errors untouched and resets the invalid flag if no other errors remain:

removeFormControlError(control: AbstractControl, errorName: string) {
  if (control?.errors && control?.errors[errorName]) {
    delete control.errors[errorName];
    if (Object.keys(control.errors).length === 0) {
      control.setErrors(null);
    }
  }
}

First, you should check your field for has this error. next, remove it. And for end you are need to update all errors in your controll.

Something like this in validator function:

if (control.hasError('firstError')) {
      delete control.errors['firstError'];
      control.updateValueAndValidity();
    }

Regards.


You can remove error with:

control.setErrors({'firstError': null});

control.updateValueAndValidity();

You can remove like this:

control.setErrors({'firstError': null})

Tags:

Angular