Skip to content

Commit 9d59b7d

Browse files
committed
fix(forms): ensure to emit statusChanges on subsequent value update/validations
This commit ensures that the `updateValueAndValidity` method takes the `asyncValidator` into consideration to emit on the `statusChanges` observables. This is necessary so that any subsequent changes are emitted properly to any subscribers. Closes #20424 Closes #14542 BREAKING CHANGE: Previously if FormControl, FormGroup and FormArray class instances had async validators defined at initialization time, the status change event was not emitted once async validator completed. After this change the status event is emitted into the `statusChanges` observable. If your code relies on the old behavior, you can filter/ignore this additional status change event.
1 parent 80cab26 commit 9d59b7d

2 files changed

Lines changed: 408 additions & 3 deletions

File tree

packages/forms/src/model.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,15 @@ export class FormControl extends AbstractControl {
11491149
super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
11501150
this._applyFormState(formState);
11511151
this._setUpdateStrategy(validatorOrOpts);
1152-
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
11531152
this._initObservables();
1153+
this.updateValueAndValidity({
1154+
onlySelf: true,
1155+
// If `asyncValidator` is present, it will trigger control status change from `PENDING` to
1156+
// `VALID` or `INVALID`.
1157+
// The status should be broadcasted via the `statusChanges` observable, so we set `emitEvent`
1158+
// to `true` to allow that during the control creation process.
1159+
emitEvent: !!asyncValidator
1160+
});
11541161
}
11551162

11561163
/**
@@ -1403,7 +1410,13 @@ export class FormGroup extends AbstractControl {
14031410
this._initObservables();
14041411
this._setUpdateStrategy(validatorOrOpts);
14051412
this._setUpControls();
1406-
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
1413+
this.updateValueAndValidity({
1414+
onlySelf: true,
1415+
// If `asyncValidator` is present, it will trigger control status change from `PENDING` to
1416+
// `VALID` or `INVALID`. The status should be broadcasted via the `statusChanges` observable,
1417+
// so we set `emitEvent` to `true` to allow that during the control creation process.
1418+
emitEvent: !!asyncValidator
1419+
});
14071420
}
14081421

14091422
/**
@@ -1823,7 +1836,14 @@ export class FormArray extends AbstractControl {
18231836
this._initObservables();
18241837
this._setUpdateStrategy(validatorOrOpts);
18251838
this._setUpControls();
1826-
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
1839+
this.updateValueAndValidity({
1840+
onlySelf: true,
1841+
// If `asyncValidator` is present, it will trigger control status change from `PENDING` to
1842+
// `VALID` or `INVALID`.
1843+
// The status should be broadcasted via the `statusChanges` observable, so we set `emitEvent`
1844+
// to `true` to allow that during the control creation process.
1845+
emitEvent: !!asyncValidator
1846+
});
18271847
}
18281848

18291849
/**

0 commit comments

Comments
 (0)