angular-docs-cn/modules/@angular/forms/src/validators.ts

176 lines
5.6 KiB
TypeScript
Raw Normal View History

/**
* @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-06-08 18:36:24 -04:00
import {OpaqueToken} from '@angular/core';
import {toPromise} from 'rxjs/operator/toPromise';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {StringMapWrapper} from './facade/collection';
import {isPresent} from './facade/lang';
import {AbstractControl} from './model';
import {isPromise} from './private_import_core';
function isEmptyInputValue(value: any): boolean {
// we don't check for string here so it also works with arrays
return value == null || value.length === 0;
}
2016-06-08 18:36:24 -04:00
/**
* Providers for validators to be used for {@link FormControl}s in a form.
2016-06-08 18:36:24 -04:00
*
* Provide this using `multi: true` to add validators.
*
* ### Example
*
* {@example core/forms/ts/ng_validators/ng_validators.ts region='ng_validators'}
* @stable
2016-06-08 18:36:24 -04:00
*/
2016-07-30 22:18:14 -04:00
export const NG_VALIDATORS: OpaqueToken = new OpaqueToken('NgValidators');
2016-06-08 18:36:24 -04:00
/**
* Providers for asynchronous validators to be used for {@link FormControl}s
2016-06-08 18:36:24 -04:00
* in a form.
*
* Provide this using `multi: true` to add validators.
*
* See {@link NG_VALIDATORS} for more details.
*
* @stable
2016-06-08 18:36:24 -04:00
*/
2016-07-30 22:18:14 -04:00
export const NG_ASYNC_VALIDATORS: OpaqueToken = new OpaqueToken('NgAsyncValidators');
2016-06-08 18:36:24 -04:00
/**
* Provides a set of validators used by form controls.
*
* A validator is a function that processes a {@link FormControl} or collection of
2016-06-08 18:36:24 -04:00
* controls and returns a map of errors. A null map means that validation has passed.
*
* ### Example
*
* ```typescript
* var loginControl = new FormControl("", Validators.required)
2016-06-08 18:36:24 -04:00
* ```
*
* @stable
2016-06-08 18:36:24 -04:00
*/
export class Validators {
/**
* Validator that requires controls to have a non-empty value.
*/
static required(control: AbstractControl): {[key: string]: boolean} {
return isEmptyInputValue(control.value) ? {'required': true} : null;
2016-06-08 18:36:24 -04:00
}
/**
* Validator that requires control value to be true.
*/
static requiredTrue(control: AbstractControl): {[key: string]: boolean} {
return control.value === true ? null : {'required': true};
}
2016-06-08 18:36:24 -04:00
/**
* Validator that requires controls to have a value of a minimum length.
*/
static minLength(minLength: number): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
const length: number = control.value ? control.value.length : 0;
return length < minLength ?
{'minlength': {'requiredLength': minLength, 'actualLength': length}} :
null;
2016-06-08 18:36:24 -04:00
};
}
/**
* Validator that requires controls to have a value of a maximum length.
*/
static maxLength(maxLength: number): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
const length: number = control.value ? control.value.length : 0;
return length > maxLength ?
{'maxlength': {'requiredLength': maxLength, 'actualLength': length}} :
null;
2016-06-08 18:36:24 -04:00
};
}
/**
* Validator that requires a control to match a regex to its value.
*/
static pattern(pattern: string|RegExp): ValidatorFn {
if (!pattern) return Validators.nullValidator;
let regex: RegExp;
let regexStr: string;
if (typeof pattern === 'string') {
regexStr = `^${pattern}$`;
regex = new RegExp(regexStr);
} else {
regexStr = pattern.toString();
regex = pattern;
}
return (control: AbstractControl): {[key: string]: any} => {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
const value: string = control.value;
return regex.test(value) ? null :
{'pattern': {'requiredPattern': regexStr, 'actualValue': value}};
2016-06-08 18:36:24 -04:00
};
}
/**
* No-op validator.
*/
static nullValidator(c: AbstractControl): {[key: string]: boolean} { return null; }
2016-06-08 18:36:24 -04:00
/**
* Compose multiple validators into a single function that returns the union
* of the individual error maps.
*/
static compose(validators: ValidatorFn[]): ValidatorFn {
if (!validators) return null;
const presentValidators = validators.filter(isPresent);
2016-06-08 18:36:24 -04:00
if (presentValidators.length == 0) return null;
return function(control: AbstractControl) {
2016-06-08 18:36:24 -04:00
return _mergeErrors(_executeValidators(control, presentValidators));
};
}
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn {
if (!validators) return null;
const presentValidators = validators.filter(isPresent);
2016-06-08 18:36:24 -04:00
if (presentValidators.length == 0) return null;
return function(control: AbstractControl) {
const promises = _executeAsyncValidators(control, presentValidators).map(_convertToPromise);
return Promise.all(promises).then(_mergeErrors);
2016-06-08 18:36:24 -04:00
};
}
}
2016-06-20 13:55:29 -04:00
function _convertToPromise(obj: any): Promise<any> {
return isPromise(obj) ? obj : toPromise.call(obj);
2016-06-08 18:36:24 -04:00
}
function _executeValidators(control: AbstractControl, validators: ValidatorFn[]): any[] {
2016-06-08 18:36:24 -04:00
return validators.map(v => v(control));
}
function _executeAsyncValidators(control: AbstractControl, validators: AsyncValidatorFn[]): any[] {
2016-06-08 18:36:24 -04:00
return validators.map(v => v(control));
}
function _mergeErrors(arrayOfErrors: any[]): {[key: string]: any} {
const res: {[key: string]: any} =
2016-06-08 18:36:24 -04:00
arrayOfErrors.reduce((res: {[key: string]: any}, errors: {[key: string]: any}) => {
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
}, {});
return Object.keys(res).length === 0 ? null : res;
2016-06-08 18:36:24 -04:00
}