You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FormGroup & FormControl statusChanges are not emitted on creation #14542
This is not a bug, the type of statusChanges is an EventEmitter which is a class that extends Subject which does not emit an initial value. If you want it to emit then call the control.updateValueAndValidity() method after subscribing to get the initial value
Reacted by Simon Weaver, Claudiu Hojda, davoskanyan, Nicholas Franceschina and cmsImagine
I know there is no initial value that is fine. But then the status changes from "PENDING" to "VALID" or "INVALID" which is not emitted.
Thanks for the hint to use updateValueAndValidity(). This updates to "PENDING" but then still the status-change is not emitted as described above and would still be "PENDING". I updated the Plunkr to make the bug more obvious.
Reacted by GanganiSon97, Jac, Jean Pierre, Elodie and cryptalith
+1 to this. There would seem to be a need to modify angular/packages/forms/src/model.ts, and have statusChanges fire on a change of pending. The plunker linked above properly demonstrates this issue, which I'm also having in my project: no way to tell on initialization of the component whether or not the form has finished its pending status.
I encounter the same issue. On page load a canActivate method checks the status of my FormGroups. As I have some async validator functions, I have to wait until the status of my FormGroup is not PENDING anymore. This does never happen, due to the above mentioned bug.
As a workaround, I now dirty check the status (not so cool):
I worked around this in my async validator by calling the parent control's updateValueAndValidity on the next tick after the asynchronous call completes.
This causes the parent control (the FormGroup) to emit a VALID status, but it doesn't cause the control itself to emit anything. If you call the control's updateValueAndValidity, that makes the async validator run again, which isn't ideal already, and worse, it creates an infinite loop. So if you need the control itself to emit, not its parent, then this doesn't help much.
I guess I also don't know how we're supposed to be handling this:
I have a directive that in ngOnInit() will subscribe to statusChanges, and when the state switches between Valid/Invalid it'll render the validation message appropriately.
This has been working fine since the async validator runs as the user changes the fields; however, I've run into a situation where I'm async validating fields as they're created in the form group (the form is created based on an existing object that may be invalid).
In my directive I can see the initial state of the control is 'PENDING', but once that completes my statusChanges subscription is not hit: is the only resolution to do some sort of setTimeout hack if it's initially pending and wait for that to complete?
edit
Here's my workaround for now:
this.formControl.statusChanges.subscribe((a) => {
this.statusChanged(a);
});
//TODO: remove this at some point
if (this.formControl.pending) {
let setInitialStatus = () => {
if (this.formControl.pending) {
t = setTimeout(setInitialStatus, 50);
} else {
this.statusChanged(this.formControl.valid ? 'VALID' : 'INVALID');
clearTimeout(t);
}
};
let t = setTimeout(setInitialStatus, 50);
}
Reacted by Simon Weaver, Sergey Shevchenko, Simon Jang, Olena Horal, Vasyl Zuziak, Andrei Zaman, Matthew Evans, Michael Trouw, Valentin Despa, Alexander Torres and 2 more
I have similar issue when user does not input anything and click submit button, my validators do not fire error messages when the message component is relying on listening the input's statusChanges.
My solution is calling the following function when the submit button is clicked, and it works:
Activity
DzmitryShylovich commentedon Feb 16, 2017
Pls provide a plunkr
MickL commentedon Feb 16, 2017
https://plnkr.co/edit/BvnnfxHK3mgNVIVI7Yhs?p=preview
=> Emitted status is not set to "VALID" on first validation on init. Only if u start typing the status is emitted correctly.
Toxicable commentedon Feb 16, 2017
This is not a bug, the type of
statusChangesis anEventEmitterwhich is a class that extendsSubjectwhich does not emit an initial value. If you want it to emit then call thecontrol.updateValueAndValidity()method after subscribing to get the initial valueMickL commentedon Feb 16, 2017
I know there is no initial value that is fine. But then the status changes from "PENDING" to "VALID" or "INVALID" which is not emitted.
Thanks for the hint to use
updateValueAndValidity(). This updates to "PENDING" but then still the status-change is not emitted as described above and would still be "PENDING". I updated the Plunkr to make the bug more obvious.MickL commentedon Feb 16, 2017
I also noticed the async validator is called 4 times on FormBuilder.group(). This would cause 4 server requests.
I updated my plunker, just check the console.
Opened another issue for this here: #14551
jackkoppa commentedon Jul 11, 2017
+1 to this. There would seem to be a need to modify
angular/packages/forms/src/model.ts, and havestatusChangesfire on a change of pending. The plunker linked above properly demonstrates this issue, which I'm also having in my project: no way to tell on initialization of the component whether or not the form has finished its pending status.RobYed commentedon Jul 12, 2017
I encounter the same issue. On page load a
canActivatemethod checks the status of myFormGroups. As I have some async validator functions, I have to wait until the status of myFormGroupis notPENDINGanymore. This does never happen, due to the above mentioned bug.As a workaround, I now dirty check the status (not so cool):
mikeperri commentedon Sep 8, 2017
I worked around this in my async validator by calling the parent control's updateValueAndValidity on the next tick after the asynchronous call completes.
This causes the parent control (the FormGroup) to emit a VALID status, but it doesn't cause the control itself to emit anything. If you call the control's updateValueAndValidity, that makes the async validator run again, which isn't ideal already, and worse, it creates an infinite loop. So if you need the control itself to emit, not its parent, then this doesn't help much.
Here's a fork of the plunker: https://plnkr.co/edit/QykVJrgM8j3EA51VU9Xl?p=preview
JohnYoungers commentedon Oct 4, 2017
I guess I also don't know how we're supposed to be handling this:
I have a directive that in ngOnInit() will subscribe to statusChanges, and when the state switches between Valid/Invalid it'll render the validation message appropriately.
This has been working fine since the async validator runs as the user changes the fields; however, I've run into a situation where I'm async validating fields as they're created in the form group (the form is created based on an existing object that may be invalid).
In my directive I can see the initial state of the control is 'PENDING', but once that completes my statusChanges subscription is not hit: is the only resolution to do some sort of setTimeout hack if it's initially pending and wait for that to complete?
edit
Here's my workaround for now:
kiancross commentedon Jan 4, 2018
Any update on this?
10 remaining items
pburgmer commentedon May 6, 2020
I also have this problem.
Dear Angular team, could you please fix it 😍
The original plunkr doesn't work anymore. I copied the code to a new stackblitz: https://stackblitz.com/edit/angular-tv8yss
test(forms): status observable with async validator should emit on in…
AndrewKushnir commentedon Jun 18, 2020
Hi, it looks like this issue is related to #20424 and might be fixed by #20806. Thank you.
johnwcb commentedon Jul 9, 2020
I have similar issue when user does not input anything and click submit button, my validators do not fire error messages when the message component is relying on listening the input's statusChanges.
My solution is calling the following function when the submit button is clicked, and it works:
nicolasguilhem commentedon Sep 1, 2020
I think I am facing this issue too.
Here is my Stackblitz : https://stackblitz.com/edit/angular-bkfctw?devtoolsheight=33&file=src/app/hello.component.ts
fix(forms): ensure to emit `statusChanges` on subsequent value update…
angular-automatic-lock-bot commentedon Oct 2, 2020
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.