2016-06-08 18:36:24 -04:00
|
|
|
import {OpaqueToken} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
|
|
|
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
|
2016-06-14 21:23:40 -04:00
|
|
|
import {ObservableWrapper} from './facade/async';
|
|
|
|
import {StringMapWrapper} from './facade/collection';
|
|
|
|
import {isBlank, isPresent, isString} from './facade/lang';
|
|
|
|
import {PromiseWrapper} from './facade/promise';
|
2016-06-08 18:36:24 -04:00
|
|
|
import * as modelModule from './model';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-14 21:23:40 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2016-06-10 14:15:59 -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'}
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
export const NG_VALIDATORS: OpaqueToken = /*@ts2dart_const*/ new OpaqueToken('NgValidators');
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
2016-06-10 14:15:59 -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.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export const NG_ASYNC_VALIDATORS: OpaqueToken =
|
2016-06-08 19:38:52 -04:00
|
|
|
/*@ts2dart_const*/ new OpaqueToken('NgAsyncValidators');
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a set of validators used by form controls.
|
|
|
|
*
|
2016-06-10 14:15:59 -04:00
|
|
|
* 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
|
2016-06-10 14:15:59 -04:00
|
|
|
* var loginControl = new FormControl("", Validators.required)
|
2016-06-08 18:36:24 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class Validators {
|
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a non-empty value.
|
|
|
|
*/
|
|
|
|
static required(control: modelModule.AbstractControl): {[key: string]: boolean} {
|
2016-06-08 19:38:52 -04:00
|
|
|
return isBlank(control.value) || (isString(control.value) && control.value == '') ?
|
|
|
|
{'required': true} :
|
|
|
|
null;
|
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: modelModule.AbstractControl): {[key: string]: any} => {
|
|
|
|
if (isPresent(Validators.required(control))) return null;
|
|
|
|
var v: string = control.value;
|
|
|
|
return v.length < minLength ?
|
2016-06-08 19:38:52 -04:00
|
|
|
{'minlength': {'requiredLength': minLength, 'actualLength': v.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: modelModule.AbstractControl): {[key: string]: any} => {
|
|
|
|
if (isPresent(Validators.required(control))) return null;
|
|
|
|
var v: string = control.value;
|
|
|
|
return v.length > maxLength ?
|
2016-06-08 19:38:52 -04:00
|
|
|
{'maxlength': {'requiredLength': maxLength, 'actualLength': v.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): ValidatorFn {
|
|
|
|
return (control: modelModule.AbstractControl): {[key: string]: any} => {
|
|
|
|
if (isPresent(Validators.required(control))) return null;
|
|
|
|
let regex = new RegExp(`^${pattern}$`);
|
|
|
|
let v: string = control.value;
|
|
|
|
return regex.test(v) ? null :
|
2016-06-08 19:38:52 -04:00
|
|
|
{'pattern': {'requiredPattern': `^${pattern}$`, 'actualValue': v}};
|
2016-06-08 18:36:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No-op validator.
|
|
|
|
*/
|
|
|
|
static nullValidator(c: modelModule.AbstractControl): {[key: string]: boolean} { return null; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose multiple validators into a single function that returns the union
|
|
|
|
* of the individual error maps.
|
|
|
|
*/
|
|
|
|
static compose(validators: ValidatorFn[]): ValidatorFn {
|
|
|
|
if (isBlank(validators)) return null;
|
|
|
|
var presentValidators = validators.filter(isPresent);
|
|
|
|
if (presentValidators.length == 0) return null;
|
|
|
|
|
|
|
|
return function(control: modelModule.AbstractControl) {
|
|
|
|
return _mergeErrors(_executeValidators(control, presentValidators));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn {
|
|
|
|
if (isBlank(validators)) return null;
|
|
|
|
var presentValidators = validators.filter(isPresent);
|
|
|
|
if (presentValidators.length == 0) return null;
|
|
|
|
|
|
|
|
return function(control: modelModule.AbstractControl) {
|
|
|
|
let promises = _executeAsyncValidators(control, presentValidators).map(_convertToPromise);
|
|
|
|
return PromiseWrapper.all(promises).then(_mergeErrors);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _convertToPromise(obj: any): any {
|
|
|
|
return PromiseWrapper.isPromise(obj) ? obj : ObservableWrapper.toPromise(obj);
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
function _executeValidators(
|
|
|
|
control: modelModule.AbstractControl, validators: ValidatorFn[]): any[] {
|
2016-06-08 18:36:24 -04:00
|
|
|
return validators.map(v => v(control));
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
function _executeAsyncValidators(
|
|
|
|
control: modelModule.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} {
|
|
|
|
var res: {[key: string]: any} =
|
|
|
|
arrayOfErrors.reduce((res: {[key: string]: any}, errors: {[key: string]: any}) => {
|
|
|
|
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
|
|
|
|
}, {});
|
|
|
|
return StringMapWrapper.isEmpty(res) ? null : res;
|
|
|
|
}
|