2015-08-20 14:28:25 -07:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/core/facade/lang';
|
2015-08-28 11:29:19 -07:00
|
|
|
import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-03-09 17:41:49 +01:00
|
|
|
import * as modelModule from './model';
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 11:15:01 -07:00
|
|
|
* Provides a set of validators used by form controls.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* var loginControl = new Control("", Validators.required)
|
|
|
|
* ```
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-03-19 14:21:40 -07:00
|
|
|
export class Validators {
|
2015-05-20 18:10:30 -07:00
|
|
|
static required(c: modelModule.Control): StringMap<string, boolean> {
|
2015-03-19 14:21:40 -07:00
|
|
|
return isBlank(c.value) || c.value == "" ? {"required": true} : null;
|
|
|
|
}
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
static nullValidator(c: any): StringMap<string, boolean> { return null; }
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
static compose(validators: Function[]): Function {
|
2015-05-20 18:10:30 -07:00
|
|
|
return function(c: modelModule.Control) {
|
2015-03-19 14:21:40 -07:00
|
|
|
var res = ListWrapper.reduce(validators, (res, validator) => {
|
|
|
|
var errors = validator(c);
|
|
|
|
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
|
|
|
|
}, {});
|
|
|
|
return StringMapWrapper.isEmpty(res) ? null : res;
|
2015-07-15 12:12:23 -07:00
|
|
|
};
|
2015-03-19 14:21:40 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
static group(c: modelModule.ControlGroup): StringMap<string, boolean> {
|
2015-03-19 14:21:40 -07:00
|
|
|
var res = {};
|
|
|
|
StringMapWrapper.forEach(c.controls, (control, name) => {
|
|
|
|
if (c.contains(name) && isPresent(control.errors)) {
|
2015-03-25 10:51:05 -07:00
|
|
|
Validators._mergeErrors(control, res);
|
2015-03-19 14:21:40 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return StringMapWrapper.isEmpty(res) ? null : res;
|
|
|
|
}
|
2015-03-25 10:51:05 -07:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
static array(c: modelModule.ControlArray): StringMap<string, boolean> {
|
2015-03-25 10:51:05 -07:00
|
|
|
var res = {};
|
|
|
|
ListWrapper.forEach(c.controls, (control) => {
|
|
|
|
if (isPresent(control.errors)) {
|
|
|
|
Validators._mergeErrors(control, res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return StringMapWrapper.isEmpty(res) ? null : res;
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:17:21 -07:00
|
|
|
static _mergeErrors(control: modelModule.AbstractControl, res: StringMap<string, any[]>): void {
|
2015-03-25 10:51:05 -07:00
|
|
|
StringMapWrapper.forEach(control.errors, (value, error) => {
|
|
|
|
if (!StringMapWrapper.contains(res, error)) {
|
|
|
|
res[error] = [];
|
|
|
|
}
|
2015-06-17 11:17:21 -07:00
|
|
|
var current: any[] = res[error];
|
|
|
|
current.push(control);
|
2015-03-25 10:51:05 -07:00
|
|
|
});
|
|
|
|
}
|
2015-02-11 11:10:31 -08:00
|
|
|
}
|