refactor(forms): wrapped all validators into the Validator class
This commit is contained in:
6
modules/angular2/src/forms/directives.js
vendored
6
modules/angular2/src/forms/directives.js
vendored
@ -4,7 +4,7 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
import {isBlank, isPresent, isString, CONST} from 'angular2/src/facade/lang';
|
||||
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {ControlGroup, Control} from './model';
|
||||
import * as validators from './validators';
|
||||
import {Validators} from './validators';
|
||||
|
||||
@CONST()
|
||||
export class ControlValueAccessor {
|
||||
@ -79,7 +79,7 @@ export class ControlDirective {
|
||||
this._el = el;
|
||||
this.controlName = null;
|
||||
this.type = null;
|
||||
this.validator = validators.nullValidator;
|
||||
this.validator = Validators.nullValidator;
|
||||
}
|
||||
|
||||
// TODO: vsavkin this should be moved into the constructor once static bindings
|
||||
@ -92,7 +92,7 @@ export class ControlDirective {
|
||||
this._groupDirective.addDirective(this);
|
||||
|
||||
var c = this._control();
|
||||
c.validator = validators.compose([c.validator, this.validator]);
|
||||
c.validator = Validators.compose([c.validator, this.validator]);
|
||||
|
||||
if (isBlank(this.valueAccessor)) {
|
||||
this.valueAccessor = controlValueAccessorFor(this.type);
|
||||
|
8
modules/angular2/src/forms/model.js
vendored
8
modules/angular2/src/forms/model.js
vendored
@ -1,6 +1,6 @@
|
||||
import {isPresent} from 'angular2/src/facade/lang';
|
||||
import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {nullValidator, controlGroupValidator} from './validators';
|
||||
import {Validators} from './validators';
|
||||
|
||||
export const VALID = "VALID";
|
||||
export const INVALID = "INVALID";
|
||||
@ -26,7 +26,7 @@ export class AbstractControl {
|
||||
_parent:ControlGroup;
|
||||
validator:Function;
|
||||
|
||||
constructor(validator:Function = nullValidator) {
|
||||
constructor(validator:Function) {
|
||||
this.validator = validator;
|
||||
this._updateNeeded = true;
|
||||
this._pristine = true;
|
||||
@ -76,7 +76,7 @@ export class AbstractControl {
|
||||
}
|
||||
|
||||
export class Control extends AbstractControl {
|
||||
constructor(value:any, validator:Function = nullValidator) {
|
||||
constructor(value:any, validator:Function = Validators.nullValidator) {
|
||||
super(validator);
|
||||
this._value = value;
|
||||
}
|
||||
@ -101,7 +101,7 @@ export class ControlGroup extends AbstractControl {
|
||||
controls;
|
||||
optionals;
|
||||
|
||||
constructor(controls, optionals = null, validator:Function = controlGroupValidator) {
|
||||
constructor(controls, optionals = null, validator:Function = Validators.group) {
|
||||
super(validator);
|
||||
this.controls = controls;
|
||||
this.optionals = isPresent(optionals) ? optionals : {};
|
||||
|
@ -1,13 +1,12 @@
|
||||
import {Decorator} from 'angular2/angular2';
|
||||
|
||||
import {ControlDirective} from 'angular2/forms';
|
||||
import * as validators from 'angular2/forms';
|
||||
import {ControlDirective, Validators} from 'angular2/forms';
|
||||
|
||||
@Decorator({
|
||||
selector: '[required]'
|
||||
})
|
||||
export class RequiredValidatorDirective {
|
||||
constructor(c:ControlDirective) {
|
||||
c.validator = validators.compose([c.validator, validators.required]);
|
||||
c.validator = Validators.compose([c.validator, Validators.required]);
|
||||
}
|
||||
}
|
56
modules/angular2/src/forms/validators.js
vendored
56
modules/angular2/src/forms/validators.js
vendored
@ -3,35 +3,37 @@ import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collectio
|
||||
|
||||
import * as modelModule from './model';
|
||||
|
||||
export function required(c:modelModule.Control) {
|
||||
return isBlank(c.value) || c.value == "" ? {"required" : true} : null;
|
||||
}
|
||||
export class Validators {
|
||||
static required(c:modelModule.Control) {
|
||||
return isBlank(c.value) || c.value == "" ? {"required": true} : null;
|
||||
}
|
||||
|
||||
export function nullValidator(c:modelModule.Control) {
|
||||
return null;
|
||||
}
|
||||
static nullValidator(c:modelModule.Control) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function compose(validators:List<Function>):Function {
|
||||
return function(c:modelModule.Control) {
|
||||
var res = ListWrapper.reduce(validators, (res, validator) => {
|
||||
var errors = validator(c);
|
||||
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
|
||||
}, {});
|
||||
static compose(validators:List<Function>):Function {
|
||||
return function (c:modelModule.Control) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static group(c:modelModule.ControlGroup) {
|
||||
var res = {};
|
||||
StringMapWrapper.forEach(c.controls, (control, name) => {
|
||||
if (c.contains(name) && isPresent(control.errors)) {
|
||||
StringMapWrapper.forEach(control.errors, (value, error) => {
|
||||
if (!StringMapWrapper.contains(res, error)) {
|
||||
res[error] = [];
|
||||
}
|
||||
ListWrapper.push(res[error], control);
|
||||
});
|
||||
}
|
||||
});
|
||||
return StringMapWrapper.isEmpty(res) ? null : res;
|
||||
}
|
||||
}
|
||||
|
||||
export function controlGroupValidator(c:modelModule.ControlGroup) {
|
||||
var res = {};
|
||||
StringMapWrapper.forEach(c.controls, (control, name) => {
|
||||
if (c.contains(name) && isPresent(control.errors)) {
|
||||
StringMapWrapper.forEach(control.errors, (value, error) => {
|
||||
if (! StringMapWrapper.contains(res, error)) {
|
||||
res[error] = [];
|
||||
}
|
||||
ListWrapper.push(res[error], control);
|
||||
});
|
||||
}
|
||||
});
|
||||
return StringMapWrapper.isEmpty(res) ? null : res;
|
||||
}
|
||||
|
Reference in New Issue
Block a user