Skip to content

Commit b716d23

Browse files
committed
fix(forms): emit value changes after errors and status are set
Closes #4714
1 parent 6436f96 commit b716d23

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ export class AbstractControl {
107107

108108
this._updateValue();
109109

110+
this._errors = this.validator(this);
111+
this._status = isPresent(this._errors) ? INVALID : VALID;
112+
110113
if (emitEvent) {
111114
ObservableWrapper.callNext(this._valueChanges, this._value);
112115
}
113116

114-
this._errors = this.validator(this);
115-
this._status = isPresent(this._errors) ? INVALID : VALID;
116-
117117
if (isPresent(this._parent) && !onlySelf) {
118118
this._parent.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
119119
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from 'angular2/testing_internal';
1616
import {ControlGroup, Control, ControlArray, Validators} from 'angular2/core';
1717
import {ObservableWrapper} from 'angular2/src/core/facade/async';
18+
import {IS_DART} from '../../platform';
1819

1920
export function main() {
2021
describe("Form Model", () => {
@@ -116,7 +117,7 @@ export function main() {
116117
describe("valueChanges", () => {
117118
var c;
118119

119-
beforeEach(() => { c = new Control("old"); });
120+
beforeEach(() => { c = new Control("old", Validators.required); });
120121

121122
it("should fire an event after the value has been updated",
122123
inject([AsyncTestCompleter], (async) => {
@@ -128,6 +129,19 @@ export function main() {
128129
c.updateValue("new");
129130
}));
130131

132+
// TODO: remove the if statement after making observable delivery sync
133+
if (!IS_DART) {
134+
it("should update set errors and status before emitting an event",
135+
inject([AsyncTestCompleter], (async) => {
136+
c.valueChanges.toRx().subscribe(value => {
137+
expect(c.valid).toEqual(false);
138+
expect(c.errors).toEqual({"required": true});
139+
async.done();
140+
});
141+
c.updateValue("");
142+
}));
143+
}
144+
131145
it("should return a cold observable", inject([AsyncTestCompleter], (async) => {
132146
c.updateValue("will be ignored");
133147
ObservableWrapper.subscribe(c.valueChanges, (value) => {

0 commit comments

Comments
 (0)