fix(forms): emit value changes after errors and status are set

Closes #4714
This commit is contained in:
vsavkin 2015-10-13 18:27:30 -07:00 committed by Victor Savkin
parent 6436f96fd1
commit b716d2335b
2 changed files with 18 additions and 4 deletions

View File

@ -107,13 +107,13 @@ export class AbstractControl {
this._updateValue();
this._errors = this.validator(this);
this._status = isPresent(this._errors) ? INVALID : VALID;
if (emitEvent) {
ObservableWrapper.callNext(this._valueChanges, this._value);
}
this._errors = this.validator(this);
this._status = isPresent(this._errors) ? INVALID : VALID;
if (isPresent(this._parent) && !onlySelf) {
this._parent.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
}

View File

@ -15,6 +15,7 @@ import {
} from 'angular2/testing_internal';
import {ControlGroup, Control, ControlArray, Validators} from 'angular2/core';
import {ObservableWrapper} from 'angular2/src/core/facade/async';
import {IS_DART} from '../../platform';
export function main() {
describe("Form Model", () => {
@ -116,7 +117,7 @@ export function main() {
describe("valueChanges", () => {
var c;
beforeEach(() => { c = new Control("old"); });
beforeEach(() => { c = new Control("old", Validators.required); });
it("should fire an event after the value has been updated",
inject([AsyncTestCompleter], (async) => {
@ -128,6 +129,19 @@ export function main() {
c.updateValue("new");
}));
// TODO: remove the if statement after making observable delivery sync
if (!IS_DART) {
it("should update set errors and status before emitting an event",
inject([AsyncTestCompleter], (async) => {
c.valueChanges.toRx().subscribe(value => {
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({"required": true});
async.done();
});
c.updateValue("");
}));
}
it("should return a cold observable", inject([AsyncTestCompleter], (async) => {
c.updateValue("will be ignored");
ObservableWrapper.subscribe(c.valueChanges, (value) => {