Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix DDC errors/warnings #7195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/angular2/src/common/forms/directives/ng_control.ts
@@ -1,6 +1,7 @@
import {ControlValueAccessor} from './control_value_accessor';
import {AbstractControlDirective} from './abstract_control_directive';
import {unimplemented} from 'angular2/src/facade/exceptions';
import {AsyncValidatorFn, ValidatorFn} from './validators';

/**
* A base class that all control directive extend.
Expand All @@ -12,8 +13,8 @@ export abstract class NgControl extends AbstractControlDirective {
name: string = null;
valueAccessor: ControlValueAccessor = null;

get validator(): Function { return unimplemented(); }
get asyncValidator(): Function { return unimplemented(); }
get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }

abstract viewToModelUpdate(newValue: any): void;
}
Expand Up @@ -16,7 +16,8 @@ import {ControlContainer} from './control_container';
import {controlPath, composeValidators, composeAsyncValidators} from './shared';
import {ControlGroup} from '../model';
import {Form} from './form_interface';
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {AsyncValidatorFn, ValidatorFn} from './validators';

const controlGroupProvider =
CONST_EXPR(new Provider(ControlContainer, {useExisting: forwardRef(() => NgControlGroup)}));
Expand Down Expand Up @@ -106,7 +107,7 @@ export class NgControlGroup extends ControlContainer implements OnInit,
*/
get formDirective(): Form { return this._parent.formDirective; }

get validator(): Function { return composeValidators(this._validators); }
get validator(): ValidatorFn { return composeValidators(this._validators); }

get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
}
Expand Up @@ -27,7 +27,8 @@ import {
selectValueAccessor
} from './shared';
import {Control} from '../model';
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {ValidatorFn, AsyncValidatorFn} from './validators';


const controlNameBinding =
Expand Down Expand Up @@ -136,9 +137,9 @@ export class NgControlName extends NgControl implements OnChanges,

get formDirective(): any { return this._parent.formDirective; }

get validator(): Function { return composeValidators(this._validators); }
get validator(): ValidatorFn { return composeValidators(this._validators); }

get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }

get control(): Control { return this.formDirective.getControl(this); }
}
Expand Up @@ -23,6 +23,7 @@ import {
isPropertyUpdated,
selectValueAccessor
} from './shared';
import {ValidatorFn, AsyncValidatorFn} from './validators';

const formControlBinding =
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgFormControl)}));
Expand Down Expand Up @@ -110,9 +111,9 @@ export class NgFormControl extends NgControl implements OnChanges {

get path(): string[] { return []; }

get validator(): Function { return composeValidators(this._validators); }
get validator(): ValidatorFn { return composeValidators(this._validators); }

get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }

get control(): Control { return this.form; }

Expand Down
8 changes: 4 additions & 4 deletions modules/angular2/src/common/forms/directives/ng_model.ts
Expand Up @@ -3,7 +3,6 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {
OnChanges,
SimpleChange,
Query,
Directive,
forwardRef,
Provider,
Expand All @@ -14,14 +13,15 @@ import {
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
import {NgControl} from './ng_control';
import {Control} from '../model';
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
import {
setUpControl,
isPropertyUpdated,
selectValueAccessor,
composeValidators,
composeAsyncValidators
} from './shared';
import {ValidatorFn, AsyncValidatorFn} from './validators';

const formControlBinding =
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgModel)}));
Expand Down Expand Up @@ -88,9 +88,9 @@ export class NgModel extends NgControl implements OnChanges {

get path(): string[] { return []; }

get validator(): Function { return composeValidators(this._validators); }
get validator(): ValidatorFn { return composeValidators(this._validators); }

get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }

viewToModelUpdate(newValue: any): void {
this.viewModel = newValue;
Expand Down
Expand Up @@ -10,3 +10,11 @@ Function normalizeValidator(dynamic validator){
}
}


Function normalizeAsyncValidator(dynamic validator){
if (validator is Validator) {
return (c) => validator.validate(c);
} else {
return validator;
}
}
@@ -1,14 +1,18 @@
import {Validator} from './validators';
import {Control} from "../model";
import {AbstractControl} from "../model";
import {Validator, ValidatorFn, AsyncValidatorFn} from './validators';

export type ctrlFunc = ((c: Control) => {
[key: string]: any
});
export function normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn {
if ((<Validator>validator).validate !== undefined) {
return (c: AbstractControl) => (<Validator>validator).validate(c);
} else {
return <ValidatorFn>validator;
}
}

export function normalizeValidator(validator: (ctrlFunc | Validator)): ctrlFunc {
export function normalizeAsyncValidator(validator: AsyncValidatorFn | Validator): AsyncValidatorFn {
if ((<Validator>validator).validate !== undefined) {
return (c: Control) => (<Validator>validator).validate(c);
return (c: AbstractControl) => Promise.resolve((<Validator>validator).validate(c));
} else {
return <ctrlFunc>validator;
return <AsyncValidatorFn>validator;
}
}
10 changes: 6 additions & 4 deletions modules/angular2/src/common/forms/directives/shared.ts
Expand Up @@ -14,7 +14,8 @@ import {NumberValueAccessor} from './number_value_accessor';
import {CheckboxControlValueAccessor} from './checkbox_value_accessor';
import {SelectControlValueAccessor} from './select_control_value_accessor';
import {RadioControlValueAccessor} from './radio_control_value_accessor';
import {normalizeValidator} from './normalize_validator';
import {normalizeValidator, normalizeAsyncValidator} from './normalize_validator';
import {ValidatorFn, AsyncValidatorFn} from './validators';


export function controlPath(name: string, parent: ControlContainer): string[] {
Expand Down Expand Up @@ -56,13 +57,14 @@ function _throwError(dir: AbstractControlDirective, message: string): void {
throw new BaseException(`${message} '${path}'`);
}

export function composeValidators(validators: /* Array<Validator|Function> */ any[]): Function {
export function composeValidators(validators: /* Array<Validator|Function> */ any[]): ValidatorFn {
return isPresent(validators) ? Validators.compose(validators.map(normalizeValidator)) : null;
}

export function composeAsyncValidators(
validators: /* Array<Validator|Function> */ any[]): Function {
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeValidator)) : null;
validators: /* Array<Validator|Function> */ any[]): AsyncValidatorFn {
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
null;
}

export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any): boolean {
Expand Down
24 changes: 15 additions & 9 deletions modules/angular2/src/common/forms/directives/validators.ts
@@ -1,11 +1,12 @@
import {forwardRef, Provider, OpaqueToken, Attribute, Directive} from 'angular2/core';
import {forwardRef, Provider, Attribute, Directive} from 'angular2/core';
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {Validators, NG_VALIDATORS} from '../validators';
import {Control} from '../model';
import {AbstractControl} from '../model';
import * as modelModule from '../model';
import {NumberWrapper} from "angular2/src/facade/lang";



/**
* An interface that can be implemented by classes that can act as validators.
*
Expand All @@ -23,7 +24,7 @@ import {NumberWrapper} from "angular2/src/facade/lang";
* }
* ```
*/
export interface Validator { validate(c: modelModule.Control): {[key: string]: any}; }
export interface Validator { validate(c: modelModule.AbstractControl): {[key: string]: any}; }

const REQUIRED_VALIDATOR =
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: Validators.required, multi: true}));
Expand All @@ -45,6 +46,11 @@ const REQUIRED_VALIDATOR =
export class RequiredValidator {
}

export interface ValidatorFn { (c: AbstractControl): {[key: string]: any}; }
export interface AsyncValidatorFn {
(c: AbstractControl): any /*Promise<{[key: string]: any}>|Observable<{[key: string]: any}>*/;
}

/**
* Provivder which adds {@link MinLengthValidator} to {@link NG_VALIDATORS}.
*
Expand All @@ -64,13 +70,13 @@ const MIN_LENGTH_VALIDATOR = CONST_EXPR(
providers: [MIN_LENGTH_VALIDATOR]
})
export class MinLengthValidator implements Validator {
private _validator: Function;
private _validator: ValidatorFn;

constructor(@Attribute("minlength") minLength: string) {
this._validator = Validators.minLength(NumberWrapper.parseInt(minLength, 10));
}

validate(c: Control): {[key: string]: any} { return this._validator(c); }
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
}

/**
Expand All @@ -92,13 +98,13 @@ const MAX_LENGTH_VALIDATOR = CONST_EXPR(
providers: [MAX_LENGTH_VALIDATOR]
})
export class MaxLengthValidator implements Validator {
private _validator: Function;
private _validator: ValidatorFn;

constructor(@Attribute("maxlength") maxLength: string) {
this._validator = Validators.maxLength(NumberWrapper.parseInt(maxLength, 10));
}

validate(c: Control): {[key: string]: any} { return this._validator(c); }
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
}


Expand All @@ -121,11 +127,11 @@ const PATTERN_VALIDATOR = CONST_EXPR(
providers: [PATTERN_VALIDATOR]
})
export class PatternValidator implements Validator {
private _validator: Function;
private _validator: ValidatorFn;

constructor(@Attribute("pattern") pattern: string) {
this._validator = Validators.pattern(pattern);
}

validate(c: Control): {[key: string]: any} { return this._validator(c); }
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
}
23 changes: 13 additions & 10 deletions modules/angular2/src/common/forms/form_builder.ts
Expand Up @@ -2,6 +2,7 @@ import {Injectable} from 'angular2/core';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {isPresent, isArray, CONST_EXPR, Type} from 'angular2/src/facade/lang';
import * as modelModule from './model';
import {ValidatorFn, AsyncValidatorFn} from './directives/validators';


/**
Expand Down Expand Up @@ -56,25 +57,27 @@ export class FormBuilder {
group(controlsConfig: {[key: string]: any},
extra: {[key: string]: any} = null): modelModule.ControlGroup {
var controls = this._reduceControls(controlsConfig);
var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null;
var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
var asyncValidator = isPresent(extra) ? StringMapWrapper.get(extra, "asyncValidator") : null;
var optionals = <{[key: string]: boolean}>(
isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null);
var validator: ValidatorFn = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
var asyncValidator: AsyncValidatorFn =
isPresent(extra) ? StringMapWrapper.get(extra, "asyncValidator") : null;
return new modelModule.ControlGroup(controls, optionals, validator, asyncValidator);
}
/**
* Construct a new {@link Control} with the given `value`,`validator`, and `asyncValidator`.
*/
control(value: Object, validator: Function = null,
asyncValidator: Function = null): modelModule.Control {
control(value: Object, validator: ValidatorFn = null,
asyncValidator: AsyncValidatorFn = null): modelModule.Control {
return new modelModule.Control(value, validator, asyncValidator);
}

/**
* Construct an array of {@link Control}s from the given `controlsConfig` array of
* configuration, with the given optional `validator` and `asyncValidator`.
*/
array(controlsConfig: any[], validator: Function = null,
asyncValidator: Function = null): modelModule.ControlArray {
array(controlsConfig: any[], validator: ValidatorFn = null,
asyncValidator: AsyncValidatorFn = null): modelModule.ControlArray {
var controls = controlsConfig.map(c => this._createControl(c));
return new modelModule.ControlArray(controls, validator, asyncValidator);
}
Expand All @@ -98,12 +101,12 @@ export class FormBuilder {

} else if (isArray(controlConfig)) {
var value = controlConfig[0];
var validator = controlConfig.length > 1 ? controlConfig[1] : null;
var asyncValidator = controlConfig.length > 2 ? controlConfig[2] : null;
var validator: ValidatorFn = controlConfig.length > 1 ? controlConfig[1] : null;
var asyncValidator: AsyncValidatorFn = controlConfig.length > 2 ? controlConfig[2] : null;
return this.control(value, validator, asyncValidator);

} else {
return this.control(controlConfig);
}
}
}
}
24 changes: 14 additions & 10 deletions modules/angular2/src/common/forms/model.ts
@@ -1,7 +1,8 @@
import {StringWrapper, isPresent, isBlank, normalizeBool} from 'angular2/src/facade/lang';
import {isPresent, isBlank, normalizeBool} from 'angular2/src/facade/lang';
import {Observable, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {PromiseWrapper} from 'angular2/src/facade/promise';
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {ValidatorFn, AsyncValidatorFn} from './directives/validators';

/**
* Indicates that a Control is valid, i.e. that no errors exist in the input value.
Expand Down Expand Up @@ -64,7 +65,7 @@ export abstract class AbstractControl {
private _parent: ControlGroup | ControlArray;
private _asyncValidationSubscription: any;

constructor(public validator: Function, public asyncValidator: Function) {}
constructor(public validator: ValidatorFn, public asyncValidator: AsyncValidatorFn) {}

get value(): any { return this._value; }

Expand Down Expand Up @@ -137,15 +138,17 @@ export abstract class AbstractControl {
}
}

private _runValidator() { return isPresent(this.validator) ? this.validator(this) : null; }
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));
this._asyncValidationSubscription =
ObservableWrapper.subscribe(obs, res => this.setErrors(res, {emitEvent: emitEvent}));
this._asyncValidationSubscription = ObservableWrapper.subscribe(
obs, (res: {[key: string]: any}) => this.setErrors(res, {emitEvent: emitEvent}));
}
}

Expand Down Expand Up @@ -268,7 +271,8 @@ export class Control extends AbstractControl {
/** @internal */
_onChange: Function;

constructor(value: any = null, validator: Function = null, asyncValidator: Function = null) {
constructor(value: any = null, validator: ValidatorFn = null,
asyncValidator: AsyncValidatorFn = null) {
super(validator, asyncValidator);
this._value = value;
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
Expand Down Expand Up @@ -331,8 +335,8 @@ export class ControlGroup extends AbstractControl {
private _optionals: {[key: string]: boolean};

constructor(public controls: {[key: string]: AbstractControl},
optionals: {[key: string]: boolean} = null, validator: Function = null,
asyncValidator: Function = null) {
optionals: {[key: string]: boolean} = null, validator: ValidatorFn = null,
asyncValidator: AsyncValidatorFn = null) {
super(validator, asyncValidator);
this._optionals = isPresent(optionals) ? optionals : {};
this._initObservables();
Expand Down Expand Up @@ -444,8 +448,8 @@ export class ControlGroup extends AbstractControl {
* ### Example ([live demo](http://plnkr.co/edit/23DESOpbNnBpBHZt1BR4?p=preview))
*/
export class ControlArray extends AbstractControl {
constructor(public controls: AbstractControl[], validator: Function = null,
asyncValidator: Function = null) {
constructor(public controls: AbstractControl[], validator: ValidatorFn = null,
asyncValidator: AsyncValidatorFn = null) {
super(validator, asyncValidator);
this._initObservables();
this._setParentForControls();
Expand Down