1

Currently I'm trying to create a dynamic validation in Angular Forms and I found this Question.

Adding the error to the field

this.form.get('field').setErrors({ 'invalid': true });

Removing the error to the field

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

My question is how can I remove a specific error in the field? Example I have multiple setErrors and I only want to remove the invalid error

1
  • I'm afraid that you can not, setError replace all the errors. You can always use setErrors(Validators.required) or to have an array of errors [Validators.required,Validators.minLenght(3)] and use setErrors(errorsArray) but I suppose it's not you want Commented Jun 24, 2020 at 6:47

3 Answers 3

2

I faced the same problem, and thought of two ways to do it:

1. Using delete

const control = this.form.get('field');
const error = 'invalid';

if (control.hasError(error)) {
  delete this.form.get('field').errors[error];
}

However, this may be slow.
You should also check that the error exists using hasError as the rest of your code may not work otherwise. I made this mistake and wasted hours trying to figure why my code wasn't working.

2. Using setErrors

const control = this.form.get('field');
const error = 'invalid';

{ [error]: _ignored, ...errors } = control.errors;
control.setErrors(Object.keys(errors).length ? errors : null);

Here I'm using ES6 destructuring assignment together with the spread syntax to get only the errors that the control should have.

Important:
When setting errors, make sure to check that errors isn't empty by checking Object.keys(errors).length is more than 0. An empty object looks like this: {}, and this may flag the field as erroneous since {} is coalesced into true.

The { [error]: _ignored, ...errors } syntax is useful for when error is a variable. I found myself using this when passing error as a parameter to a function. If 'invalid' isn't stored in a variable, you can use this instead:

{ invalid: _ignored, ...errors } = control.errors;
Sign up to request clarification or add additional context in comments.

1 Comment

When I try this using the destructuring assignment and spread operator, I get an error Property 'xyz' does not exist on type 'ValidationErrors | null'. Do you have any idea how to solve this?
1

I believe this should work:

this.form.get('field').setError({'invalid': null})

1 Comment

Hi Ana it does not work. The invalid error is set to null, but the field detect that their is an error. I want to remove only an specific error because when I use the setError(null) even the required validation is removed.
0
  1. Get the current errors
  2. Remove that specific error(s)
  3. Set the new errors
const errors = { ...this.form.get(field).errors };
// Or if you wanna be extra safe in case that errors cannot be spread
// const errors = { ...(this.form.get(field).errors || {}) };
delete errors['invalid'];
this.form.get('field').setErrors(errors);

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.