I want a way to remove a specific error from a form control and not clear all error.
control.setError({'firstError': true})
and to remove that specific error like
control.removeError({'firstError}) and not control.setError({null})
I tried
control.setError({'firstError': false})
but didn't work.
Any help. Thanks
angular 4.1.1
You can remove like this:
control.setErrors({'firstError': null})
You can remove error with:
control.setErrors({'firstError': null});
control.updateValueAndValidity();
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With