FormArray.setErrors() does not work as intended
See original GitHub issueI’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
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:
- Created 6 years ago
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @kara. I was calling
setErrors()inside a(ngModelChange)event handler, and it was not working… until I waited a tick withsetTimeout().As the documentation implies,
setErrorsshould 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 ifsetErrorsruns 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.