2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-30 15:13:38 -04:00
|
|
|
import {fromPromise} from 'rxjs/observable/fromPromise';
|
2016-08-02 18:53:34 -04:00
|
|
|
|
2016-06-13 14:27:04 -04:00
|
|
|
import {composeAsyncValidators, composeValidators} from './directives/shared';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {EventEmitter, Observable} from './facade/async';
|
2016-06-14 21:23:40 -04:00
|
|
|
import {ListWrapper, StringMapWrapper} from './facade/collection';
|
2016-08-24 19:58:43 -04:00
|
|
|
import {isBlank, isPresent, isPromise, isStringMap, normalizeBool} from './facade/lang';
|
2016-06-13 14:27:04 -04:00
|
|
|
|
2016-07-08 16:04:25 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Indicates that a FormControl is valid, i.e. that no errors exist in the input value.
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export const VALID = 'VALID';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Indicates that a FormControl is invalid, i.e. that an error exists in the input value.
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export const INVALID = 'INVALID';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Indicates that a FormControl is pending, i.e. that async validation is occurring and
|
2016-06-08 18:36:24 -04:00
|
|
|
* errors are not yet available for the input value.
|
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export const PENDING = 'PENDING';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
/**
|
|
|
|
* Indicates that a FormControl is disabled, i.e. that the control is exempt from ancestor
|
|
|
|
* calculations of validity or value.
|
|
|
|
*/
|
|
|
|
export const DISABLED = 'DISABLED';
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
export function isControl(control: Object): boolean {
|
|
|
|
return control instanceof AbstractControl;
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:22:50 -04:00
|
|
|
function _find(control: AbstractControl, path: Array<string|number>| string, delimiter: string) {
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isBlank(path)) return null;
|
|
|
|
|
|
|
|
if (!(path instanceof Array)) {
|
2016-08-01 17:22:50 -04:00
|
|
|
path = (<string>path).split(delimiter);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
if (path instanceof Array && ListWrapper.isEmpty(path)) return null;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
return (<Array<string|number>>path).reduce((v, name) => {
|
2016-06-10 14:15:59 -04:00
|
|
|
if (v instanceof FormGroup) {
|
2016-06-08 19:38:52 -04:00
|
|
|
return isPresent(v.controls[name]) ? v.controls[name] : null;
|
2016-06-10 14:15:59 -04:00
|
|
|
} else if (v instanceof FormArray) {
|
2016-06-08 19:38:52 -04:00
|
|
|
var index = <number>name;
|
|
|
|
return isPresent(v.at(index)) ? v.at(index) : null;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, control);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function toObservable(r: any): Observable<any> {
|
2016-08-30 15:13:38 -04:00
|
|
|
return isPromise(r) ? fromPromise(r) : r;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 14:27:04 -04:00
|
|
|
function coerceToValidator(validator: ValidatorFn | ValidatorFn[]): ValidatorFn {
|
|
|
|
return Array.isArray(validator) ? composeValidators(validator) : validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
function coerceToAsyncValidator(asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]):
|
|
|
|
AsyncValidatorFn {
|
|
|
|
return Array.isArray(asyncValidator) ? composeAsyncValidators(asyncValidator) : asyncValidator;
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
|
|
|
export abstract class AbstractControl {
|
|
|
|
/** @internal */
|
|
|
|
_value: any;
|
2016-09-02 18:57:35 -04:00
|
|
|
/** @internal */
|
|
|
|
_onCollectionChange = () => {};
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
private _valueChanges: EventEmitter<any>;
|
|
|
|
private _statusChanges: EventEmitter<any>;
|
|
|
|
private _status: string;
|
|
|
|
private _errors: {[key: string]: any};
|
|
|
|
private _pristine: boolean = true;
|
|
|
|
private _touched: boolean = false;
|
2016-06-10 14:15:59 -04:00
|
|
|
private _parent: FormGroup|FormArray;
|
2016-06-08 18:36:24 -04:00
|
|
|
private _asyncValidationSubscription: any;
|
|
|
|
|
|
|
|
constructor(public validator: ValidatorFn, public asyncValidator: AsyncValidatorFn) {}
|
|
|
|
|
|
|
|
get value(): any { return this._value; }
|
|
|
|
|
|
|
|
get status(): string { return this._status; }
|
|
|
|
|
|
|
|
get valid(): boolean { return this._status === VALID; }
|
|
|
|
|
2016-08-01 21:41:25 -04:00
|
|
|
get invalid(): boolean { return this._status === INVALID; }
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Returns the errors of this control.
|
|
|
|
*/
|
|
|
|
get errors(): {[key: string]: any} { return this._errors; }
|
|
|
|
|
|
|
|
get pristine(): boolean { return this._pristine; }
|
|
|
|
|
|
|
|
get dirty(): boolean { return !this.pristine; }
|
|
|
|
|
|
|
|
get touched(): boolean { return this._touched; }
|
|
|
|
|
|
|
|
get untouched(): boolean { return !this._touched; }
|
|
|
|
|
|
|
|
get valueChanges(): Observable<any> { return this._valueChanges; }
|
|
|
|
|
|
|
|
get statusChanges(): Observable<any> { return this._statusChanges; }
|
|
|
|
|
|
|
|
get pending(): boolean { return this._status == PENDING; }
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
get disabled(): boolean { return this._status === DISABLED; }
|
|
|
|
|
|
|
|
get enabled(): boolean { return this._status !== DISABLED; }
|
|
|
|
|
2016-06-23 11:18:07 -04:00
|
|
|
setAsyncValidators(newValidator: AsyncValidatorFn|AsyncValidatorFn[]): void {
|
|
|
|
this.asyncValidator = coerceToAsyncValidator(newValidator);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAsyncValidators(): void { this.asyncValidator = null; }
|
|
|
|
|
|
|
|
setValidators(newValidator: ValidatorFn|ValidatorFn[]): void {
|
|
|
|
this.validator = coerceToValidator(newValidator);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearValidators(): void { this.validator = null; }
|
|
|
|
|
2016-07-01 18:36:04 -04:00
|
|
|
markAsTouched({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
onlySelf = normalizeBool(onlySelf);
|
|
|
|
this._touched = true;
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent.markAsTouched({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
markAsDirty({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
onlySelf = normalizeBool(onlySelf);
|
|
|
|
this._pristine = false;
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent.markAsDirty({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
markAsPristine({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._pristine = true;
|
|
|
|
|
|
|
|
this._forEachChild((control: AbstractControl) => { control.markAsPristine({onlySelf: true}); });
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent._updatePristine({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markAsUntouched({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._touched = false;
|
|
|
|
|
|
|
|
this._forEachChild(
|
|
|
|
(control: AbstractControl) => { control.markAsUntouched({onlySelf: true}); });
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent._updateTouched({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
markAsPending({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
onlySelf = normalizeBool(onlySelf);
|
|
|
|
this._status = PENDING;
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent.markAsPending({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
disable({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
|
|
|
|
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
|
|
|
|
|
|
|
this._status = DISABLED;
|
2016-09-09 15:00:38 -04:00
|
|
|
this._errors = null;
|
2016-08-24 19:58:43 -04:00
|
|
|
this._forEachChild((control: AbstractControl) => { control.disable({onlySelf: true}); });
|
|
|
|
this._updateValue();
|
|
|
|
|
|
|
|
if (emitEvent) {
|
|
|
|
this._valueChanges.emit(this._value);
|
|
|
|
this._statusChanges.emit(this._status);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateAncestors(onlySelf);
|
|
|
|
this._onDisabledChange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
enable({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
|
|
|
|
this._status = VALID;
|
|
|
|
this._forEachChild((control: AbstractControl) => { control.enable({onlySelf: true}); });
|
|
|
|
this.updateValueAndValidity({onlySelf: true, emitEvent: emitEvent});
|
|
|
|
|
|
|
|
this._updateAncestors(onlySelf);
|
|
|
|
this._onDisabledChange(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _updateAncestors(onlySelf: boolean) {
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent.updateValueAndValidity();
|
|
|
|
this._parent._updatePristine();
|
|
|
|
this._parent._updateTouched();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
setParent(parent: FormGroup|FormArray): void { this._parent = parent; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
abstract setValue(value: any, options?: Object): void;
|
|
|
|
|
|
|
|
abstract patchValue(value: any, options?: Object): void;
|
2016-07-08 16:04:25 -04:00
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
abstract reset(value?: any, options?: Object): void;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
updateValueAndValidity({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}):
|
|
|
|
void {
|
2016-06-08 18:36:24 -04:00
|
|
|
onlySelf = normalizeBool(onlySelf);
|
|
|
|
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
|
|
|
|
2016-09-09 15:00:38 -04:00
|
|
|
this._setInitialStatus();
|
2016-06-08 18:36:24 -04:00
|
|
|
this._updateValue();
|
|
|
|
|
2016-09-09 15:00:38 -04:00
|
|
|
if (this.enabled) {
|
|
|
|
this._errors = this._runValidator();
|
|
|
|
this._status = this._calculateStatus();
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-09-09 15:00:38 -04:00
|
|
|
if (this._status === VALID || this._status === PENDING) {
|
|
|
|
this._runAsyncValidator(emitEvent);
|
|
|
|
}
|
2016-08-24 19:58:43 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
if (emitEvent) {
|
2016-08-02 18:53:34 -04:00
|
|
|
this._valueChanges.emit(this._value);
|
|
|
|
this._statusChanges.emit(this._status);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:33:49 -04:00
|
|
|
/** @internal */
|
|
|
|
_updateTreeValidity({emitEvent}: {emitEvent?: boolean} = {emitEvent: true}) {
|
|
|
|
this._forEachChild((ctrl: AbstractControl) => ctrl._updateTreeValidity({emitEvent}));
|
|
|
|
this.updateValueAndValidity({onlySelf: true, emitEvent});
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:00:38 -04:00
|
|
|
private _setInitialStatus() { this._status = this._allControlsDisabled() ? DISABLED : VALID; }
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
private _runValidator(): {[key: string]: any} {
|
|
|
|
return isPresent(this.validator) ? this.validator(this) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _runAsyncValidator(emitEvent: boolean): void {
|
|
|
|
if (isPresent(this.asyncValidator)) {
|
|
|
|
this._status = PENDING;
|
|
|
|
this._cancelExistingSubscription();
|
|
|
|
var obs = toObservable(this.asyncValidator(this));
|
2016-08-02 18:53:34 -04:00
|
|
|
this._asyncValidationSubscription = obs.subscribe(
|
|
|
|
{next: (res: {[key: string]: any}) => this.setErrors(res, {emitEvent: emitEvent})});
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _cancelExistingSubscription(): void {
|
|
|
|
if (isPresent(this._asyncValidationSubscription)) {
|
2016-08-02 18:53:34 -04:00
|
|
|
this._asyncValidationSubscription.unsubscribe();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Sets errors on a form control.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
|
|
|
* This is used when validations are run not automatically, but manually by the user.
|
|
|
|
*
|
|
|
|
* Calling `setErrors` will also update the validity of the parent control.
|
|
|
|
*
|
|
|
|
* ## Usage
|
|
|
|
*
|
|
|
|
* ```
|
2016-06-10 14:15:59 -04:00
|
|
|
* var login = new FormControl("someLogin");
|
2016-06-08 18:36:24 -04:00
|
|
|
* login.setErrors({
|
|
|
|
* "notUnique": true
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* expect(login.valid).toEqual(false);
|
|
|
|
* expect(login.errors).toEqual({"notUnique": true});
|
|
|
|
*
|
|
|
|
* login.updateValue("someOtherLogin");
|
|
|
|
*
|
|
|
|
* expect(login.valid).toEqual(true);
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
setErrors(errors: {[key: string]: any}, {emitEvent}: {emitEvent?: boolean} = {}): void {
|
|
|
|
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
|
|
|
|
|
|
|
this._errors = errors;
|
2016-06-27 23:01:24 -04:00
|
|
|
this._updateControlsErrors(emitEvent);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-08-01 17:22:50 -04:00
|
|
|
get(path: Array<string|number>|string): AbstractControl { return _find(this, path, '.'); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
getError(errorCode: string, path: string[] = null): any {
|
2016-08-11 23:40:46 -04:00
|
|
|
var control = isPresent(path) && !ListWrapper.isEmpty(path) ? this.get(path) : this;
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(control) && isPresent(control._errors)) {
|
|
|
|
return StringMapWrapper.get(control._errors, errorCode);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasError(errorCode: string, path: string[] = null): boolean {
|
|
|
|
return isPresent(this.getError(errorCode, path));
|
|
|
|
}
|
|
|
|
|
|
|
|
get root(): AbstractControl {
|
|
|
|
let x: AbstractControl = this;
|
|
|
|
|
|
|
|
while (isPresent(x._parent)) {
|
|
|
|
x = x._parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-06-27 23:01:24 -04:00
|
|
|
_updateControlsErrors(emitEvent: boolean): void {
|
2016-06-08 18:36:24 -04:00
|
|
|
this._status = this._calculateStatus();
|
|
|
|
|
2016-06-27 23:01:24 -04:00
|
|
|
if (emitEvent) {
|
2016-08-02 18:53:34 -04:00
|
|
|
this._statusChanges.emit(this._status);
|
2016-06-27 23:01:24 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(this._parent)) {
|
2016-06-27 23:01:24 -04:00
|
|
|
this._parent._updateControlsErrors(emitEvent);
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_initObservables() {
|
|
|
|
this._valueChanges = new EventEmitter();
|
|
|
|
this._statusChanges = new EventEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private _calculateStatus(): string {
|
2016-09-01 19:51:42 -04:00
|
|
|
if (this._allControlsDisabled()) return DISABLED;
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(this._errors)) return INVALID;
|
|
|
|
if (this._anyControlsHaveStatus(PENDING)) return PENDING;
|
|
|
|
if (this._anyControlsHaveStatus(INVALID)) return INVALID;
|
|
|
|
return VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
abstract _updateValue(): void;
|
|
|
|
|
|
|
|
/** @internal */
|
2016-07-12 18:02:25 -04:00
|
|
|
abstract _forEachChild(cb: Function): void;
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
abstract _anyControls(condition: Function): boolean;
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
/** @internal */
|
|
|
|
abstract _allControlsDisabled(): boolean;
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
/** @internal */
|
|
|
|
_anyControlsHaveStatus(status: string): boolean {
|
|
|
|
return this._anyControls((control: AbstractControl) => control.status == status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_anyControlsDirty(): boolean {
|
|
|
|
return this._anyControls((control: AbstractControl) => control.dirty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_anyControlsTouched(): boolean {
|
|
|
|
return this._anyControls((control: AbstractControl) => control.touched);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_updatePristine({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._pristine = !this._anyControlsDirty();
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent._updatePristine({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_updateTouched({onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._touched = this._anyControlsTouched();
|
|
|
|
|
|
|
|
if (isPresent(this._parent) && !onlySelf) {
|
|
|
|
this._parent._updateTouched({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_onDisabledChange(isDisabled: boolean): void {}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_isBoxedValue(formState: any): boolean {
|
|
|
|
return isStringMap(formState) && Object.keys(formState).length === 2 && 'value' in formState &&
|
|
|
|
'disabled' in formState;
|
|
|
|
}
|
2016-09-02 18:57:35 -04:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_registerOnCollectionChange(fn: () => void): void { this._onCollectionChange = fn; }
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Defines a part of a form that cannot be divided into other controls. `FormControl`s have values
|
|
|
|
* and
|
2016-06-08 18:36:24 -04:00
|
|
|
* validation state, which is determined by an optional validation function.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* `FormControl` is one of the three fundamental building blocks used to define forms in Angular,
|
|
|
|
* along
|
|
|
|
* with {@link FormGroup} and {@link FormArray}.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
|
|
|
* ## Usage
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* By default, a `FormControl` is created for every `<input>` or other form component.
|
2016-06-10 22:10:17 -04:00
|
|
|
* With {@link FormControlDirective} or {@link FormGroupDirective} an existing {@link FormControl}
|
|
|
|
* can be bound to a DOM element instead. This `FormControl` can be configured with a custom
|
2016-06-08 18:36:24 -04:00
|
|
|
* validation function.
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-06-10 14:15:59 -04:00
|
|
|
export class FormControl extends AbstractControl {
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-15 18:15:41 -04:00
|
|
|
_onChange: Function[] = [];
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
2016-08-24 19:58:43 -04:00
|
|
|
formState: any = null, validator: ValidatorFn|ValidatorFn[] = null,
|
2016-06-13 14:27:04 -04:00
|
|
|
asyncValidator: AsyncValidatorFn|AsyncValidatorFn[] = null) {
|
|
|
|
super(coerceToValidator(validator), coerceToAsyncValidator(asyncValidator));
|
2016-08-24 19:58:43 -04:00
|
|
|
this._applyFormState(formState);
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
|
|
|
this._initObservables();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -04:00
|
|
|
* Set the value of the form control to `value`.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* If `onlySelf` is `true`, this change will only affect the validation of this `FormControl`
|
2016-06-08 18:36:24 -04:00
|
|
|
* and not its parent component. If `emitEvent` is `true`, this change will cause a
|
2016-06-10 14:15:59 -04:00
|
|
|
* `valueChanges` event on the `FormControl` to be emitted. Both of these options default to
|
2016-06-08 18:36:24 -04:00
|
|
|
* `false`.
|
|
|
|
*
|
|
|
|
* If `emitModelToViewChange` is `true`, the view will be notified about the new value
|
|
|
|
* via an `onChange` event. This is the default behavior if `emitModelToViewChange` is not
|
|
|
|
* specified.
|
2016-07-12 18:02:25 -04:00
|
|
|
*
|
|
|
|
* If `emitViewToModelChange` is `true`, an ngModelChange event will be fired to update the
|
|
|
|
* model. This is the default behavior if `emitViewToModelChange` is not specified.
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-08-05 16:35:17 -04:00
|
|
|
setValue(value: any, {onlySelf, emitEvent, emitModelToViewChange, emitViewToModelChange}: {
|
2016-06-08 18:36:24 -04:00
|
|
|
onlySelf?: boolean,
|
|
|
|
emitEvent?: boolean,
|
2016-07-12 18:02:25 -04:00
|
|
|
emitModelToViewChange?: boolean,
|
|
|
|
emitViewToModelChange?: boolean
|
2016-06-08 18:36:24 -04:00
|
|
|
} = {}): void {
|
|
|
|
emitModelToViewChange = isPresent(emitModelToViewChange) ? emitModelToViewChange : true;
|
2016-07-12 18:02:25 -04:00
|
|
|
emitViewToModelChange = isPresent(emitViewToModelChange) ? emitViewToModelChange : true;
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
this._value = value;
|
2016-06-15 18:15:41 -04:00
|
|
|
if (this._onChange.length && emitModelToViewChange) {
|
2016-07-12 18:02:25 -04:00
|
|
|
this._onChange.forEach((changeFn) => changeFn(this._value, emitViewToModelChange));
|
2016-06-15 18:15:41 -04:00
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
/**
|
|
|
|
* This function is functionally the same as updateValue() at this level. It exists for
|
|
|
|
* symmetry with patchValue() on FormGroups and FormArrays, where it does behave differently.
|
|
|
|
*/
|
|
|
|
patchValue(value: any, options: {
|
|
|
|
onlySelf?: boolean,
|
|
|
|
emitEvent?: boolean,
|
|
|
|
emitModelToViewChange?: boolean,
|
|
|
|
emitViewToModelChange?: boolean
|
|
|
|
} = {}): void {
|
|
|
|
this.setValue(value, options);
|
|
|
|
}
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
reset(formState: any = null, {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._applyFormState(formState);
|
|
|
|
this.markAsPristine({onlySelf});
|
|
|
|
this.markAsUntouched({onlySelf});
|
|
|
|
this.setValue(this._value, {onlySelf});
|
2016-07-12 18:02:25 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_updateValue() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-07-12 18:02:25 -04:00
|
|
|
_anyControls(condition: Function): boolean { return false; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_allControlsDisabled(): boolean { return this.disabled; }
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Register a listener for change events.
|
|
|
|
*/
|
2016-06-15 18:15:41 -04:00
|
|
|
registerOnChange(fn: Function): void { this._onChange.push(fn); }
|
2016-07-12 18:02:25 -04:00
|
|
|
|
2016-08-25 17:37:57 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_clearChangeFns(): void {
|
|
|
|
this._onChange = [];
|
|
|
|
this._onDisabledChange = null;
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange = () => {};
|
2016-08-25 17:37:57 -04:00
|
|
|
}
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
/**
|
|
|
|
* Register a listener for disabled events.
|
|
|
|
*/
|
|
|
|
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void { this._onDisabledChange = fn; }
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_forEachChild(cb: Function): void {}
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
private _applyFormState(formState: any) {
|
|
|
|
if (this._isBoxedValue(formState)) {
|
|
|
|
this._value = formState.value;
|
|
|
|
formState.disabled ? this.disable({onlySelf: true, emitEvent: false}) :
|
|
|
|
this.enable({onlySelf: true, emitEvent: false});
|
|
|
|
} else {
|
|
|
|
this._value = formState;
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a part of a form, of fixed length, that can contain other controls.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* A `FormGroup` aggregates the values of each {@link FormControl} in the group.
|
|
|
|
* The status of a `FormGroup` depends on the status of its children.
|
2016-06-08 18:36:24 -04:00
|
|
|
* If one of the controls in a group is invalid, the entire group is invalid.
|
|
|
|
* Similarly, if a control changes its value, the entire group changes as well.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* `FormGroup` is one of the three fundamental building blocks used to define forms in Angular,
|
|
|
|
* along with {@link FormControl} and {@link FormArray}. {@link FormArray} can also contain other
|
2016-06-08 18:36:24 -04:00
|
|
|
* controls, but is of variable length.
|
|
|
|
*
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-06-10 14:15:59 -04:00
|
|
|
export class FormGroup extends AbstractControl {
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
2016-08-24 19:58:43 -04:00
|
|
|
public controls: {[key: string]: AbstractControl}, validator: ValidatorFn = null,
|
2016-08-17 10:44:39 -04:00
|
|
|
asyncValidator: AsyncValidatorFn = null) {
|
2016-06-08 18:36:24 -04:00
|
|
|
super(validator, asyncValidator);
|
|
|
|
this._initObservables();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._setUpControls();
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a control with the group's list of controls.
|
|
|
|
*/
|
2016-06-15 18:15:41 -04:00
|
|
|
registerControl(name: string, control: AbstractControl): AbstractControl {
|
|
|
|
if (this.controls[name]) return this.controls[name];
|
2016-06-08 18:36:24 -04:00
|
|
|
this.controls[name] = control;
|
|
|
|
control.setParent(this);
|
2016-09-02 18:57:35 -04:00
|
|
|
control._registerOnCollectionChange(this._onCollectionChange);
|
2016-06-15 18:15:41 -04:00
|
|
|
return control;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a control to this group.
|
|
|
|
*/
|
|
|
|
addControl(name: string, control: AbstractControl): void {
|
|
|
|
this.registerControl(name, control);
|
|
|
|
this.updateValueAndValidity();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a control from this group.
|
|
|
|
*/
|
|
|
|
removeControl(name: string): void {
|
2016-09-02 18:57:35 -04:00
|
|
|
if (this.controls[name]) this.controls[name]._registerOnCollectionChange(() => {});
|
|
|
|
StringMapWrapper.delete(this.controls, name);
|
|
|
|
this.updateValueAndValidity();
|
|
|
|
this._onCollectionChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace an existing control.
|
|
|
|
*/
|
|
|
|
setControl(name: string, control: AbstractControl): void {
|
|
|
|
if (this.controls[name]) this.controls[name]._registerOnCollectionChange(() => {});
|
2016-06-08 18:36:24 -04:00
|
|
|
StringMapWrapper.delete(this.controls, name);
|
2016-09-02 18:57:35 -04:00
|
|
|
if (control) this.registerControl(name, control);
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether there is a control with the given name in the group.
|
|
|
|
*/
|
|
|
|
contains(controlName: string): boolean {
|
2016-09-12 18:15:50 -04:00
|
|
|
return this.controls.hasOwnProperty(controlName) && this.controls[controlName].enabled;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
setValue(value: {[key: string]: any}, {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._checkAllValuesPresent(value);
|
2016-07-08 16:04:25 -04:00
|
|
|
StringMapWrapper.forEach(value, (newValue: any, name: string) => {
|
|
|
|
this._throwIfControlMissing(name);
|
2016-08-05 16:35:17 -04:00
|
|
|
this.controls[name].setValue(newValue, {onlySelf: true});
|
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
|
|
|
patchValue(value: {[key: string]: any}, {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
StringMapWrapper.forEach(value, (newValue: any, name: string) => {
|
|
|
|
if (this.controls[name]) {
|
|
|
|
this.controls[name].patchValue(newValue, {onlySelf: true});
|
|
|
|
}
|
2016-07-08 16:04:25 -04:00
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
reset(value: any = {}, {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._forEachChild((control: AbstractControl, name: string) => {
|
|
|
|
control.reset(value[name], {onlySelf: true});
|
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
this._updatePristine({onlySelf: onlySelf});
|
|
|
|
this._updateTouched({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
getRawValue(): Object {
|
|
|
|
return this._reduceChildren(
|
|
|
|
{}, (acc: {[k: string]: AbstractControl}, control: AbstractControl, name: string) => {
|
|
|
|
acc[name] = control.value;
|
|
|
|
return acc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-08 16:04:25 -04:00
|
|
|
/** @internal */
|
|
|
|
_throwIfControlMissing(name: string): void {
|
2016-08-05 16:35:17 -04:00
|
|
|
if (!Object.keys(this.controls).length) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`
|
2016-08-05 16:35:17 -04:00
|
|
|
There are no form controls registered with this group yet. If you're using ngModel,
|
|
|
|
you may want to check next tick (e.g. use setTimeout).
|
|
|
|
`);
|
|
|
|
}
|
2016-07-08 16:04:25 -04:00
|
|
|
if (!this.controls[name]) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`Cannot find form control with name: ${name}.`);
|
2016-07-08 16:04:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
/** @internal */
|
2016-07-15 19:26:19 -04:00
|
|
|
_forEachChild(cb: (v: any, k: string) => void): void {
|
|
|
|
StringMapWrapper.forEach(this.controls, cb);
|
|
|
|
}
|
2016-07-12 18:02:25 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-09-02 18:57:35 -04:00
|
|
|
_setUpControls() {
|
|
|
|
this._forEachChild((control: AbstractControl) => {
|
|
|
|
control.setParent(this);
|
|
|
|
control._registerOnCollectionChange(this._onCollectionChange);
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_updateValue() { this._value = this._reduceValue(); }
|
|
|
|
|
|
|
|
/** @internal */
|
2016-07-12 18:02:25 -04:00
|
|
|
_anyControls(condition: Function): boolean {
|
2016-06-08 18:36:24 -04:00
|
|
|
var res = false;
|
2016-07-12 18:02:25 -04:00
|
|
|
this._forEachChild((control: AbstractControl, name: string) => {
|
|
|
|
res = res || (this.contains(name) && condition(control));
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_reduceValue() {
|
|
|
|
return this._reduceChildren(
|
|
|
|
{}, (acc: {[k: string]: AbstractControl}, control: AbstractControl, name: string) => {
|
2016-08-24 19:58:43 -04:00
|
|
|
if (control.enabled || this.disabled) {
|
|
|
|
acc[name] = control.value;
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
return acc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_reduceChildren(initValue: any, fn: Function) {
|
|
|
|
var res = initValue;
|
2016-08-24 19:58:43 -04:00
|
|
|
this._forEachChild(
|
|
|
|
(control: AbstractControl, name: string) => { res = fn(res, control, name); });
|
2016-06-08 18:36:24 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-08-24 19:58:43 -04:00
|
|
|
_allControlsDisabled(): boolean {
|
|
|
|
for (let controlName of Object.keys(this.controls)) {
|
|
|
|
if (this.controls[controlName].enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 15:21:48 -04:00
|
|
|
return Object.keys(this.controls).length > 0 || this.disabled;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
2016-08-05 16:35:17 -04:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_checkAllValuesPresent(value: any): void {
|
|
|
|
this._forEachChild((control: AbstractControl, name: string) => {
|
|
|
|
if (value[name] === undefined) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`Must supply a value for form control with name: '${name}'.`);
|
2016-08-05 16:35:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a part of a form, of variable length, that can contain other controls.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* A `FormArray` aggregates the values of each {@link FormControl} in the group.
|
|
|
|
* The status of a `FormArray` depends on the status of its children.
|
2016-06-08 18:36:24 -04:00
|
|
|
* If one of the controls in a group is invalid, the entire array is invalid.
|
|
|
|
* Similarly, if a control changes its value, the entire array changes as well.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* `FormArray` is one of the three fundamental building blocks used to define forms in Angular,
|
|
|
|
* along with {@link FormControl} and {@link FormGroup}. {@link FormGroup} can also contain
|
2016-06-08 18:36:24 -04:00
|
|
|
* other controls, but is of fixed length.
|
|
|
|
*
|
|
|
|
* ## Adding or removing controls
|
|
|
|
*
|
|
|
|
* To change the controls in the array, use the `push`, `insert`, or `removeAt` methods
|
2016-06-10 14:15:59 -04:00
|
|
|
* in `FormArray` itself. These methods ensure the controls are properly tracked in the
|
2016-06-08 18:36:24 -04:00
|
|
|
* form's hierarchy. Do not modify the array of `AbstractControl`s used to instantiate
|
2016-06-10 14:15:59 -04:00
|
|
|
* the `FormArray` directly, as that will result in strange and unexpected behavior such
|
2016-06-08 18:36:24 -04:00
|
|
|
* as broken change detection.
|
|
|
|
*
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2016-06-10 14:15:59 -04:00
|
|
|
export class FormArray extends AbstractControl {
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
public controls: AbstractControl[], validator: ValidatorFn = null,
|
|
|
|
asyncValidator: AsyncValidatorFn = null) {
|
2016-06-08 18:36:24 -04:00
|
|
|
super(validator, asyncValidator);
|
|
|
|
this._initObservables();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._setUpControls();
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the {@link AbstractControl} at the given `index` in the array.
|
|
|
|
*/
|
|
|
|
at(index: number): AbstractControl { return this.controls[index]; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new {@link AbstractControl} at the end of the array.
|
|
|
|
*/
|
|
|
|
push(control: AbstractControl): void {
|
|
|
|
this.controls.push(control);
|
2016-09-02 18:57:35 -04:00
|
|
|
this._registerControl(control);
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new {@link AbstractControl} at the given `index` in the array.
|
|
|
|
*/
|
|
|
|
insert(index: number, control: AbstractControl): void {
|
|
|
|
ListWrapper.insert(this.controls, index, control);
|
2016-09-02 18:57:35 -04:00
|
|
|
this._registerControl(control);
|
2016-06-08 18:36:24 -04:00
|
|
|
this.updateValueAndValidity();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the control at the given `index` in the array.
|
|
|
|
*/
|
|
|
|
removeAt(index: number): void {
|
2016-09-02 18:57:35 -04:00
|
|
|
if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {});
|
2016-06-08 18:36:24 -04:00
|
|
|
ListWrapper.removeAt(this.controls, index);
|
|
|
|
this.updateValueAndValidity();
|
2016-09-02 18:57:35 -04:00
|
|
|
this._onCollectionChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace an existing control.
|
|
|
|
*/
|
|
|
|
setControl(index: number, control: AbstractControl): void {
|
|
|
|
if (this.controls[index]) this.controls[index]._registerOnCollectionChange(() => {});
|
|
|
|
ListWrapper.removeAt(this.controls, index);
|
|
|
|
|
|
|
|
if (control) {
|
|
|
|
ListWrapper.insert(this.controls, index, control);
|
|
|
|
this._registerControl(control);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateValueAndValidity();
|
|
|
|
this._onCollectionChange();
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Length of the control array.
|
|
|
|
*/
|
|
|
|
get length(): number { return this.controls.length; }
|
|
|
|
|
2016-08-05 16:35:17 -04:00
|
|
|
setValue(value: any[], {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._checkAllValuesPresent(value);
|
2016-07-08 16:04:25 -04:00
|
|
|
value.forEach((newValue: any, index: number) => {
|
|
|
|
this._throwIfControlMissing(index);
|
2016-08-05 16:35:17 -04:00
|
|
|
this.at(index).setValue(newValue, {onlySelf: true});
|
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
|
|
|
patchValue(value: any[], {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
value.forEach((newValue: any, index: number) => {
|
|
|
|
if (this.at(index)) {
|
|
|
|
this.at(index).patchValue(newValue, {onlySelf: true});
|
|
|
|
}
|
2016-07-08 16:04:25 -04:00
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
reset(value: any = [], {onlySelf}: {onlySelf?: boolean} = {}): void {
|
|
|
|
this._forEachChild((control: AbstractControl, index: number) => {
|
|
|
|
control.reset(value[index], {onlySelf: true});
|
|
|
|
});
|
|
|
|
this.updateValueAndValidity({onlySelf: onlySelf});
|
|
|
|
this._updatePristine({onlySelf: onlySelf});
|
|
|
|
this._updateTouched({onlySelf: onlySelf});
|
|
|
|
}
|
|
|
|
|
2016-08-24 19:58:43 -04:00
|
|
|
getRawValue(): any[] { return this.controls.map((control) => control.value); }
|
|
|
|
|
2016-07-08 16:04:25 -04:00
|
|
|
/** @internal */
|
|
|
|
_throwIfControlMissing(index: number): void {
|
2016-08-05 16:35:17 -04:00
|
|
|
if (!this.controls.length) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`
|
2016-08-05 16:35:17 -04:00
|
|
|
There are no form controls registered with this array yet. If you're using ngModel,
|
|
|
|
you may want to check next tick (e.g. use setTimeout).
|
|
|
|
`);
|
|
|
|
}
|
2016-07-08 16:04:25 -04:00
|
|
|
if (!this.at(index)) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`Cannot find form control at index ${index}`);
|
2016-07-08 16:04:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:02:25 -04:00
|
|
|
/** @internal */
|
|
|
|
_forEachChild(cb: Function): void {
|
|
|
|
this.controls.forEach((control: AbstractControl, index: number) => { cb(control, index); });
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-08-24 19:58:43 -04:00
|
|
|
_updateValue(): void {
|
|
|
|
this._value = this.controls.filter((control) => control.enabled || this.disabled)
|
|
|
|
.map((control) => control.value);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/** @internal */
|
2016-07-12 18:02:25 -04:00
|
|
|
_anyControls(condition: Function): boolean {
|
2016-08-24 19:58:43 -04:00
|
|
|
return this.controls.some((control: AbstractControl) => control.enabled && condition(control));
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2016-09-02 18:57:35 -04:00
|
|
|
_setUpControls(): void {
|
|
|
|
this._forEachChild((control: AbstractControl) => this._registerControl(control));
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
2016-08-05 16:35:17 -04:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_checkAllValuesPresent(value: any): void {
|
|
|
|
this._forEachChild((control: AbstractControl, i: number) => {
|
|
|
|
if (value[i] === undefined) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`Must supply a value for form control at index: ${i}.`);
|
2016-08-05 16:35:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
_allControlsDisabled(): boolean {
|
|
|
|
for (let control of this.controls) {
|
|
|
|
if (control.enabled) return false;
|
|
|
|
}
|
2016-09-08 15:21:48 -04:00
|
|
|
return this.controls.length > 0 || this.disabled;
|
2016-08-24 19:58:43 -04:00
|
|
|
}
|
2016-09-02 18:57:35 -04:00
|
|
|
|
|
|
|
private _registerControl(control: AbstractControl) {
|
|
|
|
control.setParent(this);
|
|
|
|
control._registerOnCollectionChange(this._onCollectionChange);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|