Skip to content

Commit

Permalink
feat(forms): implement setErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Oct 26, 2015
1 parent 28db864 commit cb05dbc
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 101 deletions.
46 changes: 44 additions & 2 deletions modules/angular2/src/core/forms/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {StringWrapper, isPresent, isBlank, normalizeBool} from 'angular2/src/core/facade/lang';
import {Observable, EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {StringMapWrapper, ListWrapper} from 'angular2/src/core/facade/collection';
import {Validators} from './validators';

Expand Down Expand Up @@ -46,7 +47,7 @@ function _find(control: AbstractControl, path: Array<string | number>| string) {
/**
*
*/
export class AbstractControl {
export abstract class AbstractControl {
/** @internal */
_value: any;
/** @internal */
Expand Down Expand Up @@ -151,7 +152,8 @@ export class AbstractControl {
}

/** @internal */
_updateValue(): void {}
abstract _updateValue(): void;
abstract setErrors(errors: {[key: string]: any}): void;
}

/**
Expand Down Expand Up @@ -203,6 +205,16 @@ export class Control extends AbstractControl {
this.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
}

/**
* @internal
*/
_updateValue() {}

setErrors(errors: {[key: string]: any}): void {
this._errors = errors;
this._status = isPresent(this._errors) ? INVALID : VALID;
}

/**
* Register a listener for change events.
*/
Expand Down Expand Up @@ -258,6 +270,21 @@ export class ControlGroup extends AbstractControl {
return c && this._included(controlName);
}

setErrors(errors: {[key: string]: any}): void {
this._errors = errors;
this._status = isPresent(this._errors) ? INVALID : VALID;

if (StringMapWrapper.contains(errors, "controls")) {
var c = errors["controls"];
StringMapWrapper.forEach(c, (controlErrors, name) => {
if (!StringMapWrapper.contains(this.controls, name)) {
throw new BaseException(`Cannot find control '${name}'`);
}
this.controls[name].setErrors(controlErrors);
});
}
}

/** @internal */
_setParentForControls() {
StringMapWrapper.forEach(this.controls, (control, name) => { control.setParent(this); });
Expand Down Expand Up @@ -360,6 +387,21 @@ export class ControlArray extends AbstractControl {
*/
get length(): number { return this.controls.length; }

setErrors(errors: {[key: string]: any}): void {
this._errors = errors;
this._status = isPresent(this._errors) ? INVALID : VALID;

if (StringMapWrapper.contains(errors, "controls")) {
var c = errors["controls"];
ListWrapper.forEachWithIndex(c, (controlErrors, index) => {
if (index >= this.controls.length) {
throw new BaseException(`Cannot find control at index ${index}`);
}
this.controls[index].setErrors(controlErrors);
});
}
}

/** @internal */
_updateValue(): void { this._value = this.controls.map((control) => control.value); }

Expand Down
Loading

0 comments on commit cb05dbc

Please sign in to comment.