Skip to content

Feature: Add methods to better handle errors in AbstractControl #17090

@dev054

Description

@dev054

I'm submitting a ... (check one with "x")

Current behavior

Let's suppose that I have the following control:

const field = new FormControl('', [Validators.required]);

To remove an error I have to do something like this:

delete field.errors['required']; // Exception if it's null/undefined

The safe way:

if (field.errors) {
  delete field.errors['required'];
}

Actually to add an error I have to get what errors I have before add a new using setErrors or I'll overwrite the whole errors.

So, I have to do something like this:

const errors = field.errors;
const newErrors = { email: true };

if (errors) {
  newErrors = Object.assign(errors, newErrors);
}

field.setErrors(newErrors);

Expected behavior
Adding the methods addErrors and removeErrors in AbstractControl we could have a really better way to manipulate this.

Cleaner syntax:

addError**s**(obj);

removeError**s**(obj);

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 2.0.X
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version =

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions