Repository navigation
fix(forms): ensure to emit status change events on subsequent value-update / validations #20806
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1022,8 +1022,11 @@ export class FormControl extends AbstractControl { | |
| coerceToAsyncValidator(asyncValidator, validatorOrOpts)); | ||
| this._applyFormState(formState); | ||
| this._setUpdateStrategy(validatorOrOpts); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| this._initObservables(); | ||
| this.updateValueAndValidity({ | ||
| onlySelf: true, | ||
| emitEvent: asyncValidator ? true : false | ||
| }); // `asyncValidator` will cause status change, and that needs to be notified | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd propose moving the comment above the corresponding line + use The same/similar comment can be added to other places as well (FormGroup and FormArray). |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1272,7 +1275,10 @@ export class FormGroup extends AbstractControl { | |
| this._initObservables(); | ||
| this._setUpdateStrategy(validatorOrOpts); | ||
| this._setUpControls(); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| this.updateValueAndValidity({ | ||
| onlySelf: true, | ||
| emitEvent: asyncValidator ? true : false | ||
| }); //`asyncValidator` will cause status change, and that needs to be notified | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1689,7 +1695,10 @@ export class FormArray extends AbstractControl { | |
| this._initObservables(); | ||
| this._setUpdateStrategy(validatorOrOpts); | ||
| this._setUpControls(); | ||
| this.updateValueAndValidity({onlySelf: true, emitEvent: false}); | ||
| this.updateValueAndValidity({ | ||
| onlySelf: true, | ||
| emitEvent: asyncValidator ? true : false | ||
| }); // `asyncValidator` will cause status change, and that needs to be notified | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1298,6 +1298,36 @@ import {of } from 'rxjs'; | |
| expect(logger).toEqual(['change!']); | ||
| }); | ||
|
|
||
| it('should only emit (`VALID`) status change event in absence of `FormControl.asyncValidator`', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding tests 👍 I think we should increase test coverage a bit more to handle additional cases, for example:
Once we have improved coverage, we can also see what the difference is in what we emit into the Please let me know if any additional information is required.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick comment to the previous one: I think it'd also be great to have a test that would replace/remove a control (via
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| fakeAsync(() => { | ||
| const logger: string[] = []; | ||
| const c2 = new FormControl('new!'); | ||
| g.statusChanges.subscribe((status) => logger.push(status)); | ||
| g.setControl('one', c2); | ||
| tick(); | ||
| expect(logger).toEqual(['VALID']); | ||
| })); | ||
|
|
||
| it('should emit (`PENDING, VALID`) status change events, if `FormControl.asyncValidator` leads to success', | ||
| fakeAsync(() => { | ||
| const logger: string[] = []; | ||
| const c2 = new FormControl('new!', null, asyncValidator('new!')); | ||
| g.statusChanges.subscribe((status) => logger.push(status)); | ||
| g.setControl('one', c2); | ||
| tick(); | ||
| expect(logger).toEqual(['PENDING', 'VALID']); | ||
| })); | ||
|
|
||
| it('should emit (`PENDING, INVALID`) status change eventse, if `FormControl.asyncValidator` leads to errors', | ||
| fakeAsync(() => { | ||
| const logger: string[] = []; | ||
| const c2 = new FormControl('new!', null, asyncValidator('expected')); | ||
| g.statusChanges.subscribe((status) => logger.push(status)); | ||
| g.setControl('one', c2); | ||
| tick(); | ||
| expect(logger).toEqual(['PENDING', 'INVALID']); | ||
| })); | ||
|
|
||
| }); | ||
|
|
||
| describe('pending', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what others think, but I would be tempted to make this shorter by using
!!asyncValidator.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer shorter version as well 👍