Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormControl.errorsSubject is undefined when using asyncValidator #91

Closed
ltoenjes opened this issue Jun 9, 2021 · 8 comments · Fixed by #98
Closed

FormControl.errorsSubject is undefined when using asyncValidator #91

ltoenjes opened this issue Jun 9, 2021 · 8 comments · Fixed by #98

Comments

@ltoenjes
Copy link

ltoenjes commented Jun 9, 2021

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

When instantiating a FormGroup with asyncValidators an error occurs.

ERROR TypeError: Cannot read property 'next' of undefined at FormControl.setErrors (ngneat-reactive-forms.js:368) at SafeSubscriber._next (forms.js:3241) at SafeSubscriber.__tryOrUnsub (Subscriber.js:183) at SafeSubscriber.next (Subscriber.js:122) at Subscriber._next (Subscriber.js:72) at Subscriber.next (Subscriber.js:49) at Observable._subscribe (subscribeToArray.js:3) at Observable._trySubscribe (Observable.js:42) at Observable.subscribe (Observable.js:28) at FormControl._runAsyncValidator (forms.js:3236)

Expected behavior

Should work. Does so when using default angular FormBuilder.

Minimal reproduction of the problem with instructions

Created a form group with some sync and some async validators (which are fetching stuff from the API)

Environment


Angular version: 11.2.0


Browser:
- [x] Chrome (desktop) version 91.0.4472.77
- [ ] 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: 12.16.1  
- Platform:   Windows

Others:

@NetanelBasal
Copy link
Member

Can you please reproduce on stackblitz?

@ltoenjes
Copy link
Author

Will do.

@C0ZEN
Copy link
Contributor

C0ZEN commented Jul 27, 2021

FYI: I encountered the same issue @NetanelBasal.
I can reproduce it with a form that has one form control and one async validator (in an array).
The async validator in this case is returning an observable which is instantly resolved with a of(null).
As a workaround, using a delay is just enough to not see the error in the console.
I guess the errorsSubject is optional during a little time frame.

Note that even if an error is thrown, it's not breaking the app, so it's just annoying, nothing else.

The error is thrown from this source:

setErrors(errors, opts = {}) {
  this.errorsSubject.next(errors);
  return super.setErrors(errors, opts);
}

The errorsSubject is undefined.

@NetanelBasal
Copy link
Member

Can you please reproduce on stackblitz?

@C0ZEN
Copy link
Contributor

C0ZEN commented Jul 27, 2021

@C0ZEN
Copy link
Contributor

C0ZEN commented Jul 27, 2021

Actually, since your custom control is using the super from the extended Angular class, the latest will call the setErrors method before continuing the rest of the constructor which set the subject.
Kind of easy to come up with a fast fix, I can provide a PR and you will be the judge over the quality ;)
BTW: I tried to reproduce the issue through some tests but didn't come up with a reproduction :/

@C0ZEN
Copy link
Contributor

C0ZEN commented Jul 28, 2021

Tested and I can confirm it's good.
Thank you for the fast reactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants