Skip to content

Commit c9fba3f

Browse files
TedSandervsavkin
authored andcommitted
feat(validators): Add a pending state to AbstractControl
Add a pending state to AbstractControl and a function to set that state on themselves and their parents. This will be used for both individual async validators and when the imperitive mode is used. [Design Doc](https://docs.google.com/document/d/1EnJ3-_iFpVKFz1ifN1LkXSGQ7h3A72OQGry2g8eo7IA/edit?pli=1#heading=h.j53rt81eegm4)
1 parent 04b4035 commit c9fba3f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

modules/angular2/src/core/forms/model.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export const VALID = "VALID";
1313
*/
1414
export const INVALID = "INVALID";
1515

16+
/**
17+
* Indicates that a Control is pending, i.e. that async validation is occuring and
18+
* errors are not yet available for the input value.
19+
*/
20+
export const PENDING = "PENDING";
21+
1622
export function isControl(control: Object): boolean {
1723
return control instanceof AbstractControl;
1824
}
@@ -75,6 +81,7 @@ export class AbstractControl {
7581
get untouched(): boolean { return !this._touched; }
7682

7783
get valueChanges(): Observable { return this._valueChanges; }
84+
get pending(): boolean { return this._status == PENDING; }
7885

7986
markAsTouched(): void { this._touched = true; }
8087

@@ -87,6 +94,15 @@ export class AbstractControl {
8794
}
8895
}
8996

97+
markAsPending({onlySelf}: {onlySelf?: boolean} = {}): void {
98+
onlySelf = normalizeBool(onlySelf);
99+
this._status = PENDING;
100+
101+
if (isPresent(this._parent) && !onlySelf) {
102+
this._parent.markAsPending({onlySelf: onlySelf});
103+
}
104+
}
105+
90106
setParent(parent: ControlGroup | ControlArray): void { this._parent = parent; }
91107

92108
updateValidity({onlySelf}: {onlySelf?: boolean} = {}): void {

modules/angular2/test/core/forms/model_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,24 @@ export function main() {
479479
});
480480
});
481481

482+
describe("pending", () => {
483+
var c: Control;
484+
var a: ControlArray;
485+
486+
beforeEach(() => {
487+
c = new Control('value');
488+
a = new ControlArray([c]);
489+
});
490+
491+
it("should be false after creating a control", () => { expect(a.pending).toEqual(false); });
492+
493+
it("should be false after changing the value of the control", () => {
494+
c.markAsPending();
495+
496+
expect(a.pending).toEqual(true);
497+
});
498+
});
499+
482500
describe("valueChanges", () => {
483501
var a: ControlArray;
484502
var c1, c2;

modules/angular2/test/public_api_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ var NG_API = [
4747
'AbstractControl.getError()',
4848
'AbstractControl.hasError()',
4949
'AbstractControl.markAsDirty()',
50+
'AbstractControl.markAsPending()',
5051
'AbstractControl.markAsTouched()',
52+
'AbstractControl.pending',
5153
'AbstractControl.pristine',
5254
'AbstractControl.setParent()',
5355
'AbstractControl.status',
@@ -277,7 +279,9 @@ var NG_API = [
277279
'Control.getError()',
278280
'Control.hasError()',
279281
'Control.markAsDirty()',
282+
'Control.markAsPending()',
280283
'Control.markAsTouched()',
284+
'Control.pending',
281285
'Control.pristine',
282286
'Control.registerOnChange()',
283287
'Control.setParent()',
@@ -304,7 +308,9 @@ var NG_API = [
304308
'ControlArray.insert()',
305309
'ControlArray.length',
306310
'ControlArray.markAsDirty()',
311+
'ControlArray.markAsPending()',
307312
'ControlArray.markAsTouched()',
313+
'ControlArray.pending',
308314
'ControlArray.pristine',
309315
'ControlArray.push()',
310316
'ControlArray.removeAt()',
@@ -345,7 +351,9 @@ var NG_API = [
345351
'ControlGroup.hasError()',
346352
'ControlGroup.include()',
347353
'ControlGroup.markAsDirty()',
354+
'ControlGroup.markAsPending()',
348355
'ControlGroup.markAsTouched()',
356+
'ControlGroup.pending',
349357
'ControlGroup.pristine',
350358
'ControlGroup.removeControl()',
351359
'ControlGroup.setParent()',

0 commit comments

Comments
 (0)