angular2
Version:
Angular 2 - a web framework for modern web apps
113 lines • 4.2 kB
JavaScript
;var lang_1 = require('angular2/src/facade/lang');
var promise_1 = require('angular2/src/facade/promise');
var async_1 = require('angular2/src/facade/async');
var collection_1 = require('angular2/src/facade/collection');
var di_1 = require('angular2/src/core/di');
/**
* Providers for validators to be used for {@link Control}s in a form.
*
* Provide this using `multi: true` to add validators.
*
* ### Example
*
* ```typescript
* var providers = [
* new Provider(NG_VALIDATORS, {useValue: myValidator, multi: true})
* ];
* ```
*/
exports.NG_VALIDATORS = lang_1.CONST_EXPR(new di_1.OpaqueToken("NgValidators"));
exports.NG_ASYNC_VALIDATORS = lang_1.CONST_EXPR(new di_1.OpaqueToken("NgAsyncValidators"));
/**
* Provides a set of validators used by form controls.
*
* A validator is a function that processes a {@link Control} or collection of
* controls and returns a {@link StringMap} of errors. A null map means that
* validation has passed.
*
* # Example
*
* ```typescript
* var loginControl = new Control("", Validators.required)
* ```
*/
var Validators = (function () {
function Validators() {
}
/**
* Validator that requires controls to have a non-empty value.
*/
Validators.required = function (control) {
return lang_1.isBlank(control.value) || control.value == "" ? { "required": true } : null;
};
/**
* Validator that requires controls to have a value of a minimum length.
*/
Validators.minLength = function (minLength) {
return function (control) {
if (lang_1.isPresent(Validators.required(control)))
return null;
var v = control.value;
return v.length < minLength ?
{ "minlength": { "requiredLength": minLength, "actualLength": v.length } } :
null;
};
};
/**
* Validator that requires controls to have a value of a maximum length.
*/
Validators.maxLength = function (maxLength) {
return function (control) {
if (lang_1.isPresent(Validators.required(control)))
return null;
var v = control.value;
return v.length > maxLength ?
{ "maxlength": { "requiredLength": maxLength, "actualLength": v.length } } :
null;
};
};
/**
* No-op validator.
*/
Validators.nullValidator = function (c) { return null; };
/**
* Compose multiple validators into a single function that returns the union
* of the individual error maps.
*/
Validators.compose = function (validators) {
if (lang_1.isBlank(validators))
return null;
var presentValidators = validators.filter(lang_1.isPresent);
if (presentValidators.length == 0)
return null;
return function (control) {
return _mergeErrors(_executeValidators(control, presentValidators));
};
};
Validators.composeAsync = function (validators) {
if (lang_1.isBlank(validators))
return null;
var presentValidators = validators.filter(lang_1.isPresent);
if (presentValidators.length == 0)
return null;
return function (control) {
var promises = _executeValidators(control, presentValidators).map(_convertToPromise);
return promise_1.PromiseWrapper.all(promises).then(_mergeErrors);
};
};
return Validators;
})();
exports.Validators = Validators;
function _convertToPromise(obj) {
return promise_1.PromiseWrapper.isPromise(obj) ? obj : async_1.ObservableWrapper.toPromise(obj);
}
function _executeValidators(control, validators) {
return validators.map(function (v) { return v(control); });
}
function _mergeErrors(arrayOfErrors) {
var res = arrayOfErrors.reduce(function (res, errors) {
return lang_1.isPresent(errors) ? collection_1.StringMapWrapper.merge(res, errors) : res;
}, {});
return collection_1.StringMapWrapper.isEmpty(res) ? null : res;
}
//# sourceMappingURL=validators.js.map