Revert "fix(forms): Update types for TypeScript nullability support"
This reverts commit 6649743a2d.
Broke in G3.
This commit is contained in:
@@ -18,49 +18,45 @@ import {ValidationErrors} from './validators';
|
||||
* @stable
|
||||
*/
|
||||
export abstract class AbstractControlDirective {
|
||||
abstract get control(): AbstractControl|null;
|
||||
get control(): AbstractControl { throw new Error('unimplemented'); }
|
||||
|
||||
get value(): any { return this.control ? this.control.value : null; }
|
||||
|
||||
get valid(): boolean|null { return this.control ? this.control.valid : null; }
|
||||
get valid(): boolean { return this.control ? this.control.valid : null; }
|
||||
|
||||
get invalid(): boolean|null { return this.control ? this.control.invalid : null; }
|
||||
get invalid(): boolean { return this.control ? this.control.invalid : null; }
|
||||
|
||||
get pending(): boolean|null { return this.control ? this.control.pending : null; }
|
||||
get pending(): boolean { return this.control ? this.control.pending : null; }
|
||||
|
||||
get errors(): ValidationErrors|null { return this.control ? this.control.errors : null; }
|
||||
|
||||
get pristine(): boolean|null { return this.control ? this.control.pristine : null; }
|
||||
get pristine(): boolean { return this.control ? this.control.pristine : null; }
|
||||
|
||||
get dirty(): boolean|null { return this.control ? this.control.dirty : null; }
|
||||
get dirty(): boolean { return this.control ? this.control.dirty : null; }
|
||||
|
||||
get touched(): boolean|null { return this.control ? this.control.touched : null; }
|
||||
get touched(): boolean { return this.control ? this.control.touched : null; }
|
||||
|
||||
get untouched(): boolean|null { return this.control ? this.control.untouched : null; }
|
||||
get untouched(): boolean { return this.control ? this.control.untouched : null; }
|
||||
|
||||
get disabled(): boolean|null { return this.control ? this.control.disabled : null; }
|
||||
get disabled(): boolean { return this.control ? this.control.disabled : null; }
|
||||
|
||||
get enabled(): boolean|null { return this.control ? this.control.enabled : null; }
|
||||
get enabled(): boolean { return this.control ? this.control.enabled : null; }
|
||||
|
||||
get statusChanges(): Observable<any>|null {
|
||||
return this.control ? this.control.statusChanges : null;
|
||||
}
|
||||
get statusChanges(): Observable<any> { return this.control ? this.control.statusChanges : null; }
|
||||
|
||||
get valueChanges(): Observable<any>|null {
|
||||
return this.control ? this.control.valueChanges : null;
|
||||
}
|
||||
get valueChanges(): Observable<any> { return this.control ? this.control.valueChanges : null; }
|
||||
|
||||
get path(): string[]|null { return null; }
|
||||
get path(): string[] { return null; }
|
||||
|
||||
reset(value: any = undefined): void {
|
||||
if (this.control) this.control.reset(value);
|
||||
}
|
||||
|
||||
hasError(errorCode: string, path?: string[]): boolean {
|
||||
hasError(errorCode: string, path: string[] = null): boolean {
|
||||
return this.control ? this.control.hasError(errorCode, path) : false;
|
||||
}
|
||||
|
||||
getError(errorCode: string, path?: string[]): any {
|
||||
getError(errorCode: string, path: string[] = null): any {
|
||||
return this.control ? this.control.getError(errorCode, path) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective !.addFormGroup(this);
|
||||
this.formDirective.addFormGroup(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
@@ -46,7 +46,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
/**
|
||||
* Get the {@link FormGroup} backing this binding.
|
||||
*/
|
||||
get control(): FormGroup { return this.formDirective !.getFormGroup(this); }
|
||||
get control(): FormGroup { return this.formDirective.getFormGroup(this); }
|
||||
|
||||
/**
|
||||
* Get the path to this control group.
|
||||
@@ -56,13 +56,11 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
/**
|
||||
* Get the {@link Form} to which this group belongs.
|
||||
*/
|
||||
get formDirective(): Form|null { return this._parent ? this._parent.formDirective : null; }
|
||||
get formDirective(): Form { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._validators); }
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._asyncValidators);
|
||||
}
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
|
||||
/** @internal */
|
||||
_checkParentType(): void {}
|
||||
|
||||
@@ -17,16 +17,16 @@ import {Form} from './form_interface';
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
export abstract class ControlContainer extends AbstractControlDirective {
|
||||
export class ControlContainer extends AbstractControlDirective {
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Get the form to which this container belongs.
|
||||
*/
|
||||
get formDirective(): Form|null { return null; }
|
||||
get formDirective(): Form { return null; }
|
||||
|
||||
/**
|
||||
* Get the path to this container.
|
||||
*/
|
||||
get path(): string[]|null { return null; }
|
||||
get path(): string[] { return null; }
|
||||
}
|
||||
|
||||
@@ -26,16 +26,16 @@ function unimplemented(): any {
|
||||
*/
|
||||
export abstract class NgControl extends AbstractControlDirective {
|
||||
/** @internal */
|
||||
_parent: ControlContainer|null = null;
|
||||
name: string|null = null;
|
||||
valueAccessor: ControlValueAccessor|null = null;
|
||||
_parent: ControlContainer = null;
|
||||
name: string = null;
|
||||
valueAccessor: ControlValueAccessor = null;
|
||||
/** @internal */
|
||||
_rawValidators: Array<Validator|ValidatorFn> = [];
|
||||
/** @internal */
|
||||
_rawAsyncValidators: Array<AsyncValidator|AsyncValidatorFn> = [];
|
||||
|
||||
get validator(): ValidatorFn|null { return <ValidatorFn>unimplemented(); }
|
||||
get asyncValidator(): AsyncValidatorFn|null { return <AsyncValidatorFn>unimplemented(); }
|
||||
get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
|
||||
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }
|
||||
|
||||
abstract viewToModelUpdate(newValue: any): void;
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ export class NgForm extends ControlContainer implements Form {
|
||||
|
||||
updateModel(dir: NgControl, value: any): void {
|
||||
resolvedPromise.then(() => {
|
||||
const ctrl = <FormControl>this.form.get(dir.path !);
|
||||
const ctrl = <FormControl>this.form.get(dir.path);
|
||||
ctrl.setValue(value);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -158,9 +158,9 @@ export class NgModel extends NgControl implements OnChanges,
|
||||
|
||||
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ export class NgModel extends NgControl implements OnChanges,
|
||||
}
|
||||
|
||||
private _isStandalone(): boolean {
|
||||
return !this._parent || !!(this.options && this.options.standalone);
|
||||
return !this._parent || (this.options && this.options.standalone);
|
||||
}
|
||||
|
||||
private _setUpStandalone(): void {
|
||||
|
||||
@@ -47,7 +47,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
|
||||
}
|
||||
|
||||
registerOnChange(fn: (_: number|null) => void): void {
|
||||
registerOnChange(fn: (_: number) => void): void {
|
||||
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
||||
}
|
||||
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
|
||||
|
||||
@@ -45,7 +45,7 @@ export class RangeValueAccessor implements ControlValueAccessor {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
|
||||
}
|
||||
|
||||
registerOnChange(fn: (_: number|null) => void): void {
|
||||
registerOnChange(fn: (_: number) => void): void {
|
||||
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
||||
}
|
||||
|
||||
|
||||
@@ -88,8 +88,8 @@ export class FormControlDirective extends NgControl implements OnChanges {
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if (this._isControlChanged(changes)) {
|
||||
setUpControl(this.form, this);
|
||||
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
|
||||
this.valueAccessor !.setDisabledState !(true);
|
||||
if (this.control.disabled && this.valueAccessor.setDisabledState) {
|
||||
this.valueAccessor.setDisabledState(true);
|
||||
}
|
||||
this.form.updateValueAndValidity({emitEvent: false});
|
||||
}
|
||||
@@ -101,9 +101,9 @@ export class FormControlDirective extends NgControl implements OnChanges {
|
||||
|
||||
get path(): string[] { return []; }
|
||||
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
}
|
||||
|
||||
|
||||
@@ -125,14 +125,14 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
|
||||
this.update.emit(newValue);
|
||||
}
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent !); }
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
|
||||
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
return composeAsyncValidators(this._rawAsyncValidators) !;
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
}
|
||||
|
||||
get control(): FormControl { return this._control; }
|
||||
@@ -151,8 +151,8 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
|
||||
private _setUpControl() {
|
||||
this._checkParentType();
|
||||
this._control = this.formDirective.addControl(this);
|
||||
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
|
||||
this.valueAccessor !.setDisabledState !(true);
|
||||
if (this.control.disabled && this.valueAccessor.setDisabledState) {
|
||||
this.valueAccessor.setDisabledState(true);
|
||||
}
|
||||
this._added = true;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ export class FormGroupDirective extends ControlContainer implements Form,
|
||||
private _oldForm: FormGroup;
|
||||
directives: FormControlName[] = [];
|
||||
|
||||
@Input('formGroup') form: FormGroup = null !;
|
||||
@Input('formGroup') form: FormGroup = null;
|
||||
@Output() ngSubmit = new EventEmitter();
|
||||
|
||||
constructor(
|
||||
@@ -167,10 +167,10 @@ export class FormGroupDirective extends ControlContainer implements Form,
|
||||
|
||||
private _updateValidators() {
|
||||
const sync = composeValidators(this._validators);
|
||||
this.form.validator = Validators.compose([this.form.validator !, sync !]);
|
||||
this.form.validator = Validators.compose([this.form.validator, sync]);
|
||||
|
||||
const async = composeAsyncValidators(this._asyncValidators);
|
||||
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator !, async !]);
|
||||
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator, async]);
|
||||
}
|
||||
|
||||
private _checkFormPresent() {
|
||||
|
||||
@@ -166,7 +166,7 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective !.addFormArray(this);
|
||||
this.formDirective.addFormArray(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
@@ -175,19 +175,17 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
|
||||
}
|
||||
}
|
||||
|
||||
get control(): FormArray { return this.formDirective !.getFormArray(this); }
|
||||
get control(): FormArray { return this.formDirective.getFormArray(this); }
|
||||
|
||||
get formDirective(): FormGroupDirective|null {
|
||||
get formDirective(): FormGroupDirective {
|
||||
return this._parent ? <FormGroupDirective>this._parent.formDirective : null;
|
||||
}
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._validators); }
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._asyncValidators);
|
||||
}
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
|
||||
private _checkParentType(): void {
|
||||
if (_hasInvalidParent(this._parent)) {
|
||||
|
||||
@@ -15,7 +15,7 @@ export const SELECT_VALUE_ACCESSOR: Provider = {
|
||||
multi: true
|
||||
};
|
||||
|
||||
function _buildValueString(id: string | null, value: any): string {
|
||||
function _buildValueString(id: string, value: any): string {
|
||||
if (id == null) return `${value}`;
|
||||
if (value && typeof value === 'object') value = 'Object';
|
||||
return `${id}: ${value}`.slice(0, 50);
|
||||
@@ -118,7 +118,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
|
||||
|
||||
writeValue(value: any): void {
|
||||
this.value = value;
|
||||
const id: string|null = this._getOptionId(value);
|
||||
const id: string = this._getOptionId(value);
|
||||
if (id == null) {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'selectedIndex', -1);
|
||||
}
|
||||
@@ -142,7 +142,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
|
||||
_registerOption(): string { return (this._idCounter++).toString(); }
|
||||
|
||||
/** @internal */
|
||||
_getOptionId(value: any): string|null {
|
||||
_getOptionId(value: any): string {
|
||||
for (const id of Array.from(this._optionMap.keys())) {
|
||||
if (this._compareWith(this._optionMap.get(id), value)) return id;
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_getOptionId(value: any): string|null {
|
||||
_getOptionId(value: any): string {
|
||||
for (const id of Array.from(this._optionMap.keys())) {
|
||||
if (this._compareWith(this._optionMap.get(id) !._value, value)) return id;
|
||||
if (this._compareWith(this._optionMap.get(id)._value, value)) return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
|
||||
/** @internal */
|
||||
_getOptionValue(valueString: string): any {
|
||||
const id: string = _extractId(valueString);
|
||||
return this._optionMap.has(id) ? this._optionMap.get(id) !._value : valueString;
|
||||
return this._optionMap.has(id) ? this._optionMap.get(id)._value : valueString;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,55 +27,55 @@ import {AsyncValidator, AsyncValidatorFn, Validator, ValidatorFn} from './valida
|
||||
|
||||
|
||||
export function controlPath(name: string, parent: ControlContainer): string[] {
|
||||
return [...parent.path !, name];
|
||||
return [...parent.path, name];
|
||||
}
|
||||
|
||||
export function setUpControl(control: FormControl, dir: NgControl): void {
|
||||
if (!control) _throwError(dir, 'Cannot find control with');
|
||||
if (!dir.valueAccessor) _throwError(dir, 'No value accessor for form control with');
|
||||
|
||||
control.validator = Validators.compose([control.validator !, dir.validator]);
|
||||
control.asyncValidator = Validators.composeAsync([control.asyncValidator !, dir.asyncValidator]);
|
||||
dir.valueAccessor !.writeValue(control.value);
|
||||
control.validator = Validators.compose([control.validator, dir.validator]);
|
||||
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
|
||||
dir.valueAccessor.writeValue(control.value);
|
||||
|
||||
// view -> model
|
||||
dir.valueAccessor !.registerOnChange((newValue: any) => {
|
||||
dir.valueAccessor.registerOnChange((newValue: any) => {
|
||||
dir.viewToModelUpdate(newValue);
|
||||
control.markAsDirty();
|
||||
control.setValue(newValue, {emitModelToViewChange: false});
|
||||
});
|
||||
|
||||
// touched
|
||||
dir.valueAccessor !.registerOnTouched(() => control.markAsTouched());
|
||||
dir.valueAccessor.registerOnTouched(() => control.markAsTouched());
|
||||
|
||||
control.registerOnChange((newValue: any, emitModelEvent: boolean) => {
|
||||
// control -> view
|
||||
dir.valueAccessor !.writeValue(newValue);
|
||||
dir.valueAccessor.writeValue(newValue);
|
||||
|
||||
// control -> ngModel
|
||||
if (emitModelEvent) dir.viewToModelUpdate(newValue);
|
||||
});
|
||||
|
||||
if (dir.valueAccessor !.setDisabledState) {
|
||||
if (dir.valueAccessor.setDisabledState) {
|
||||
control.registerOnDisabledChange(
|
||||
(isDisabled: boolean) => { dir.valueAccessor !.setDisabledState !(isDisabled); });
|
||||
(isDisabled: boolean) => { dir.valueAccessor.setDisabledState(isDisabled); });
|
||||
}
|
||||
|
||||
// re-run validation when validator binding changes, e.g. minlength=3 -> minlength=4
|
||||
dir._rawValidators.forEach((validator: Validator | ValidatorFn) => {
|
||||
if ((<Validator>validator).registerOnValidatorChange)
|
||||
(<Validator>validator).registerOnValidatorChange !(() => control.updateValueAndValidity());
|
||||
(<Validator>validator).registerOnValidatorChange(() => control.updateValueAndValidity());
|
||||
});
|
||||
|
||||
dir._rawAsyncValidators.forEach((validator: AsyncValidator | AsyncValidatorFn) => {
|
||||
if ((<Validator>validator).registerOnValidatorChange)
|
||||
(<Validator>validator).registerOnValidatorChange !(() => control.updateValueAndValidity());
|
||||
(<Validator>validator).registerOnValidatorChange(() => control.updateValueAndValidity());
|
||||
});
|
||||
}
|
||||
|
||||
export function cleanUpControl(control: FormControl, dir: NgControl) {
|
||||
dir.valueAccessor !.registerOnChange(() => _noControlError(dir));
|
||||
dir.valueAccessor !.registerOnTouched(() => _noControlError(dir));
|
||||
dir.valueAccessor.registerOnChange(() => _noControlError(dir));
|
||||
dir.valueAccessor.registerOnTouched(() => _noControlError(dir));
|
||||
|
||||
dir._rawValidators.forEach((validator: any) => {
|
||||
if (validator.registerOnValidatorChange) {
|
||||
@@ -105,9 +105,9 @@ function _noControlError(dir: NgControl) {
|
||||
|
||||
function _throwError(dir: AbstractControlDirective, message: string): void {
|
||||
let messageEnd: string;
|
||||
if (dir.path !.length > 1) {
|
||||
messageEnd = `path: '${dir.path!.join(' -> ')}'`;
|
||||
} else if (dir.path ![0]) {
|
||||
if (dir.path.length > 1) {
|
||||
messageEnd = `path: '${dir.path.join(' -> ')}'`;
|
||||
} else if (dir.path[0]) {
|
||||
messageEnd = `name: '${dir.path}'`;
|
||||
} else {
|
||||
messageEnd = 'unspecified name attribute';
|
||||
@@ -115,12 +115,11 @@ function _throwError(dir: AbstractControlDirective, message: string): void {
|
||||
throw new Error(`${message} ${messageEnd}`);
|
||||
}
|
||||
|
||||
export function composeValidators(validators: Array<Validator|Function>): ValidatorFn|null {
|
||||
export function composeValidators(validators: Array<Validator|Function>): ValidatorFn {
|
||||
return validators != null ? Validators.compose(validators.map(normalizeValidator)) : null;
|
||||
}
|
||||
|
||||
export function composeAsyncValidators(validators: Array<Validator|Function>): AsyncValidatorFn|
|
||||
null {
|
||||
export function composeAsyncValidators(validators: Array<Validator|Function>): AsyncValidatorFn {
|
||||
return validators != null ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
|
||||
null;
|
||||
}
|
||||
@@ -148,12 +147,12 @@ export function isBuiltInAccessor(valueAccessor: ControlValueAccessor): boolean
|
||||
|
||||
// TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
|
||||
export function selectValueAccessor(
|
||||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null {
|
||||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor {
|
||||
if (!valueAccessors) return null;
|
||||
|
||||
let defaultAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let builtinAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let customAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let defaultAccessor: ControlValueAccessor;
|
||||
let builtinAccessor: ControlValueAccessor;
|
||||
let customAccessor: ControlValueAccessor;
|
||||
valueAccessors.forEach((v: ControlValueAccessor) => {
|
||||
if (v.constructor === DefaultValueAccessor) {
|
||||
defaultAccessor = v;
|
||||
|
||||
@@ -39,7 +39,7 @@ export class FormBuilder {
|
||||
*
|
||||
* See the {@link FormGroup} constructor for more details.
|
||||
*/
|
||||
group(controlsConfig: {[key: string]: any}, extra: {[key: string]: any}|null = null): FormGroup {
|
||||
group(controlsConfig: {[key: string]: any}, extra: {[key: string]: any} = null): FormGroup {
|
||||
const controls = this._reduceControls(controlsConfig);
|
||||
const validator: ValidatorFn = extra != null ? extra['validator'] : null;
|
||||
const asyncValidator: AsyncValidatorFn = extra != null ? extra['asyncValidator'] : null;
|
||||
@@ -54,8 +54,8 @@ export class FormBuilder {
|
||||
*
|
||||
*/
|
||||
control(
|
||||
formState: Object, validator?: ValidatorFn|ValidatorFn[]|null,
|
||||
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl {
|
||||
formState: Object, validator: ValidatorFn|ValidatorFn[] = null,
|
||||
asyncValidator: AsyncValidatorFn|AsyncValidatorFn[] = null): FormControl {
|
||||
return new FormControl(formState, validator, asyncValidator);
|
||||
}
|
||||
|
||||
@@ -64,8 +64,8 @@ export class FormBuilder {
|
||||
* configuration, with the given optional `validator` and `asyncValidator`.
|
||||
*/
|
||||
array(
|
||||
controlsConfig: any[], validator?: ValidatorFn|null,
|
||||
asyncValidator?: AsyncValidatorFn|null): FormArray {
|
||||
controlsConfig: any[], validator: ValidatorFn = null,
|
||||
asyncValidator: AsyncValidatorFn = null): FormArray {
|
||||
const controls = controlsConfig.map(c => this._createControl(c));
|
||||
return new FormArray(controls, validator, asyncValidator);
|
||||
}
|
||||
|
||||
+24
-23
@@ -42,7 +42,7 @@ function _find(control: AbstractControl, path: Array<string|number>| string, del
|
||||
}
|
||||
if (path instanceof Array && (path.length === 0)) return null;
|
||||
|
||||
return (<Array<string|number>>path).reduce((v: AbstractControl, name) => {
|
||||
return (<Array<string|number>>path).reduce((v, name) => {
|
||||
if (v instanceof FormGroup) {
|
||||
return v.controls[name] || null;
|
||||
}
|
||||
@@ -55,14 +55,13 @@ function _find(control: AbstractControl, path: Array<string|number>| string, del
|
||||
}, control);
|
||||
}
|
||||
|
||||
function coerceToValidator(validator?: ValidatorFn | ValidatorFn[] | null): ValidatorFn|null {
|
||||
return Array.isArray(validator) ? composeValidators(validator) : validator || null;
|
||||
function coerceToValidator(validator: ValidatorFn | ValidatorFn[]): ValidatorFn {
|
||||
return Array.isArray(validator) ? composeValidators(validator) : validator;
|
||||
}
|
||||
|
||||
function coerceToAsyncValidator(asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null):
|
||||
AsyncValidatorFn|null {
|
||||
return Array.isArray(asyncValidator) ? composeAsyncValidators(asyncValidator) :
|
||||
asyncValidator || null;
|
||||
function coerceToAsyncValidator(asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]):
|
||||
AsyncValidatorFn {
|
||||
return Array.isArray(asyncValidator) ? composeAsyncValidators(asyncValidator) : asyncValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +90,7 @@ export abstract class AbstractControl {
|
||||
private _parent: FormGroup|FormArray;
|
||||
private _asyncValidationSubscription: any;
|
||||
|
||||
constructor(public validator: ValidatorFn|null, public asyncValidator: AsyncValidatorFn|null) {}
|
||||
constructor(public validator: ValidatorFn, public asyncValidator: AsyncValidatorFn) {}
|
||||
|
||||
/**
|
||||
* The value of the control.
|
||||
@@ -210,7 +209,7 @@ export abstract class AbstractControl {
|
||||
* Sets the synchronous validators that are active on this control. Calling
|
||||
* this will overwrite any existing sync validators.
|
||||
*/
|
||||
setValidators(newValidator: ValidatorFn|ValidatorFn[]|null): void {
|
||||
setValidators(newValidator: ValidatorFn|ValidatorFn[]): void {
|
||||
this.validator = coerceToValidator(newValidator);
|
||||
}
|
||||
|
||||
@@ -323,7 +322,7 @@ export abstract class AbstractControl {
|
||||
this._statusChanges.emit(this._status);
|
||||
}
|
||||
|
||||
this._updateAncestors(!!onlySelf);
|
||||
this._updateAncestors(onlySelf);
|
||||
this._onDisabledChange.forEach((changeFn) => changeFn(true));
|
||||
}
|
||||
|
||||
@@ -339,7 +338,7 @@ export abstract class AbstractControl {
|
||||
this._forEachChild((control: AbstractControl) => { control.enable({onlySelf: true}); });
|
||||
this.updateValueAndValidity({onlySelf: true, emitEvent});
|
||||
|
||||
this._updateAncestors(!!onlySelf);
|
||||
this._updateAncestors(onlySelf);
|
||||
this._onDisabledChange.forEach((changeFn) => changeFn(false));
|
||||
}
|
||||
|
||||
@@ -410,7 +409,7 @@ export abstract class AbstractControl {
|
||||
return this.validator ? this.validator(this) : null;
|
||||
}
|
||||
|
||||
private _runAsyncValidator(emitEvent?: boolean): void {
|
||||
private _runAsyncValidator(emitEvent: boolean): void {
|
||||
if (this.asyncValidator) {
|
||||
this._status = PENDING;
|
||||
const obs = toObservable(this.asyncValidator(this));
|
||||
@@ -466,7 +465,7 @@ export abstract class AbstractControl {
|
||||
*
|
||||
* * `this.form.get(['person', 'name']);`
|
||||
*/
|
||||
get(path: Array<string|number>|string): AbstractControl|null { return _find(this, path, '.'); }
|
||||
get(path: Array<string|number>|string): AbstractControl { return _find(this, path, '.'); }
|
||||
|
||||
/**
|
||||
* Returns true if the control with the given path has the error specified. Otherwise
|
||||
@@ -474,7 +473,7 @@ export abstract class AbstractControl {
|
||||
*
|
||||
* If no path is given, it checks for the error on the present control.
|
||||
*/
|
||||
getError(errorCode: string, path?: string[]): any {
|
||||
getError(errorCode: string, path: string[] = null): any {
|
||||
const control = path ? this.get(path) : this;
|
||||
return control && control._errors ? control._errors[errorCode] : null;
|
||||
}
|
||||
@@ -485,7 +484,9 @@ export abstract class AbstractControl {
|
||||
*
|
||||
* If no path is given, it checks for the error on the present control.
|
||||
*/
|
||||
hasError(errorCode: string, path?: string[]): boolean { return !!this.getError(errorCode, path); }
|
||||
hasError(errorCode: string, path: string[] = null): boolean {
|
||||
return !!this.getError(errorCode, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the top-level ancestor of this control.
|
||||
@@ -634,8 +635,8 @@ export class FormControl extends AbstractControl {
|
||||
_onChange: Function[] = [];
|
||||
|
||||
constructor(
|
||||
formState: any = null, validator?: ValidatorFn|ValidatorFn[]|null,
|
||||
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null) {
|
||||
formState: any = null, validator: ValidatorFn|ValidatorFn[] = null,
|
||||
asyncValidator: AsyncValidatorFn|AsyncValidatorFn[] = null) {
|
||||
super(coerceToValidator(validator), coerceToAsyncValidator(asyncValidator));
|
||||
this._applyFormState(formState);
|
||||
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
||||
@@ -830,9 +831,9 @@ export class FormControl extends AbstractControl {
|
||||
*/
|
||||
export class FormGroup extends AbstractControl {
|
||||
constructor(
|
||||
public controls: {[key: string]: AbstractControl}, validator?: ValidatorFn|null,
|
||||
asyncValidator?: AsyncValidatorFn|null) {
|
||||
super(validator || null, asyncValidator || null);
|
||||
public controls: {[key: string]: AbstractControl}, validator: ValidatorFn = null,
|
||||
asyncValidator: AsyncValidatorFn = null) {
|
||||
super(validator, asyncValidator);
|
||||
this._initObservables();
|
||||
this._setUpControls();
|
||||
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
||||
@@ -1136,9 +1137,9 @@ export class FormGroup extends AbstractControl {
|
||||
*/
|
||||
export class FormArray extends AbstractControl {
|
||||
constructor(
|
||||
public controls: AbstractControl[], validator?: ValidatorFn|null,
|
||||
asyncValidator?: AsyncValidatorFn|null) {
|
||||
super(validator || null, asyncValidator || null);
|
||||
public controls: AbstractControl[], validator: ValidatorFn = null,
|
||||
asyncValidator: AsyncValidatorFn = null) {
|
||||
super(validator, asyncValidator);
|
||||
this._initObservables();
|
||||
this._setUpControls();
|
||||
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
|
||||
|
||||
@@ -143,11 +143,9 @@ export class Validators {
|
||||
* Compose multiple validators into a single function that returns the union
|
||||
* of the individual error maps.
|
||||
*/
|
||||
static compose(validators: null): null;
|
||||
static compose(validators: (ValidatorFn|null|undefined)[]): ValidatorFn|null;
|
||||
static compose(validators: (ValidatorFn|null|undefined)[]|null): ValidatorFn|null {
|
||||
static compose(validators: ValidatorFn[]): ValidatorFn {
|
||||
if (!validators) return null;
|
||||
const presentValidators: ValidatorFn[] = validators.filter(isPresent) as any;
|
||||
const presentValidators = validators.filter(isPresent);
|
||||
if (presentValidators.length == 0) return null;
|
||||
|
||||
return function(control: AbstractControl) {
|
||||
@@ -155,9 +153,9 @@ export class Validators {
|
||||
};
|
||||
}
|
||||
|
||||
static composeAsync(validators: (AsyncValidatorFn|null)[]): AsyncValidatorFn|null {
|
||||
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn {
|
||||
if (!validators) return null;
|
||||
const presentValidators: AsyncValidatorFn[] = validators.filter(isPresent) as any;
|
||||
const presentValidators = validators.filter(isPresent);
|
||||
if (presentValidators.length == 0) return null;
|
||||
|
||||
return function(control: AbstractControl) {
|
||||
@@ -190,7 +188,7 @@ function _executeAsyncValidators(control: AbstractControl, validators: AsyncVali
|
||||
function _mergeErrors(arrayOfErrors: ValidationErrors[]): ValidationErrors|null {
|
||||
const res: {[key: string]: any} =
|
||||
arrayOfErrors.reduce((res: ValidationErrors | null, errors: ValidationErrors | null) => {
|
||||
return errors != null ? {...res !, ...errors} : res !;
|
||||
return errors != null ? {...res, ...errors} : res;
|
||||
}, {});
|
||||
return Object.keys(res).length === 0 ? null : res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user