Skip to content

Commit 7714d6a

Browse files
committed
fix(forms): avoid issues with nulls checking on validation status and other form states.
Closes #4338
1 parent 1f2302e commit 7714d6a

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import {AbstractControl} from '../model';
2+
import {isPresent} from 'angular2/src/core/facade/lang';
23

34
export class AbstractControlDirective {
45
get control(): AbstractControl { return null; }
56

6-
get value(): any { return this.control.value; }
7+
get value(): any { return isPresent(this.control) ? this.control.value : null; }
78

8-
get valid(): boolean { return this.control.valid; }
9+
get valid(): boolean { return isPresent(this.control) ? this.control.valid : null; }
910

10-
get errors(): StringMap<string, any> { return this.control.errors; }
11+
get errors(): StringMap<string, any> {
12+
return isPresent(this.control) ? this.control.errors : null;
13+
}
1114

12-
get pristine(): boolean { return this.control.pristine; }
15+
get pristine(): boolean { return isPresent(this.control) ? this.control.pristine : null; }
1316

14-
get dirty(): boolean { return this.control.dirty; }
17+
get dirty(): boolean { return isPresent(this.control) ? this.control.dirty : null; }
1518

16-
get touched(): boolean { return this.control.touched; }
19+
get touched(): boolean { return isPresent(this.control) ? this.control.touched : null; }
1720

18-
get untouched(): boolean { return this.control.untouched; }
21+
get untouched(): boolean { return isPresent(this.control) ? this.control.untouched : null; }
1922
}

‎modules/angular2/test/core/forms/integration_spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,19 @@ export function main() {
800800
rootTC.detectChanges();
801801
expect(input.value).toEqual("aa");
802802
})));
803+
it("should not crash when validity is checked from a binding",
804+
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
805+
// {{x.valid}} used to crash because valid() tried to read a property
806+
// from form.control before it was set. This test verifies this bug is
807+
// fixed.
808+
var t = `<form><div ng-control-group="x" #x="form">
809+
<input type="text" ng-control="test"></div>{{x.valid}}</form>`;
810+
var rootTC: RootTestComponent;
811+
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
812+
(root) => { rootTC = root; });
813+
tick();
814+
rootTC.detectChanges();
815+
})));
803816
});
804817
});
805818
}

0 commit comments

Comments
 (0)