FormArray.setErrors() does not work as intended

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Setting formArray.setErrors(); does not set an error.

Expected behavior

The formArray should populate the .errors property and update its valid state accordingly.

Minimal reproduction of the problem with instructions

Plnkr

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

I’m implementing a validation that requires certain fields of a FormArray to be unique. If they are not, an error should be displayed on the FormArray level. The documentation clearly states that FormArray inherits .setErrors() from AbstractControl.

Environment


Angular version: 4.3.6


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
mrajcokcommented, Apr 14, 2018

Thanks @kara. I was calling setErrors() inside a (ngModelChange) event handler, and it was not working… until I waited a tick with setTimeout().

2reactions
karacommented, Nov 2, 2017

As the documentation implies,setErrors should indeed set the errors. The problem in your example is that you’re setting the errors in the constructor, before validation has a chance to run in your form control directives. Once that validation happens, your errors will be overwritten. You can see in the updated plunker that if setErrors runs later, the errors update as expected (http://plnkr.co/edit/j1HHJwky17x79d5T4Ln2?p=preview). If you need to set errors as soon as the form loads, you can normally achieve this by waiting a tick for the next change detection run.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular: setErrors not working inside loop - Stack Overflow
I'm iterating over the FormArray and for each element ( FormGroup ), I'm checking a condition, disabling the valid FormGroup s to avoid...
Read more >
FormArray - Angular
FormArray accepts one generic argument, which is the type of the controls inside. If you need a heterogenous array, use UntypedFormArray .
Read more >
Unit testing using Reactive forms in Angular. - Medium
Test Case 1: Testing the number of elements rendered in UI are equals to the form controls defined in the reactive form.
Read more >
Convert $form_state to an object and provide methods like ...
Updated: Comment #38 Problem/Motivation Looking at validation ... Convert $form_state to an object and provide methods like setError().
Read more >
How to add error to child form controls in angular without ...
In your case, I think you meant setErrors and not setError ? ... answering the more general problem of setting errors without possibly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found