@ -12,7 +12,7 @@ import {
|
||||
SelectControlValueAccessor,
|
||||
NgSelectOption
|
||||
} from './directives/select_control_value_accessor';
|
||||
import {DefaultValidators} from './directives/validators';
|
||||
import {RequiredValidator, MinLengthValidator, MaxLengthValidator} from './directives/validators';
|
||||
|
||||
export {NgControlName} from './directives/ng_control_name';
|
||||
export {NgFormControl} from './directives/ng_form_control';
|
||||
@ -28,7 +28,7 @@ export {
|
||||
SelectControlValueAccessor,
|
||||
NgSelectOption
|
||||
} from './directives/select_control_value_accessor';
|
||||
export {DefaultValidators} from './directives/validators';
|
||||
export {RequiredValidator, MinLengthValidator, MaxLengthValidator} from './directives/validators';
|
||||
export {NgControlStatus} from './directives/ng_control_status';
|
||||
|
||||
/**
|
||||
@ -62,5 +62,7 @@ export const FORM_DIRECTIVES: Type[] = CONST_EXPR([
|
||||
SelectControlValueAccessor,
|
||||
NgControlStatus,
|
||||
|
||||
DefaultValidators
|
||||
RequiredValidator,
|
||||
MinLengthValidator,
|
||||
MaxLengthValidator
|
||||
]);
|
||||
|
@ -1,14 +1,53 @@
|
||||
import {forwardRef, Provider, OpaqueToken} from 'angular2/src/core/di';
|
||||
import {CONST_EXPR} from 'angular2/src/core/facade/lang';
|
||||
import {Directive} from 'angular2/src/core/metadata';
|
||||
import {Attribute, Directive} from 'angular2/src/core/metadata';
|
||||
import {Validators, NG_VALIDATORS} from '../validators';
|
||||
import {NumberWrapper} from "angular2/src/core/facade/lang";
|
||||
|
||||
const DEFAULT_VALIDATORS =
|
||||
const REQUIRED_VALIDATOR =
|
||||
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: Validators.required, multi: true}));
|
||||
|
||||
@Directive({
|
||||
selector: '[required][ng-control],[required][ng-form-control],[required][ng-model]',
|
||||
bindings: [DEFAULT_VALIDATORS]
|
||||
providers: [REQUIRED_VALIDATOR]
|
||||
})
|
||||
export class DefaultValidators {
|
||||
export class RequiredValidator {
|
||||
}
|
||||
|
||||
function createMinLengthValidator(dir): any {
|
||||
return Validators.minLength(dir.minLength);
|
||||
}
|
||||
const MIN_LENGTH_VALIDATOR = CONST_EXPR(new Provider(NG_VALIDATORS, {
|
||||
useFactory: createMinLengthValidator,
|
||||
deps: [forwardRef(() => MinLengthValidator)],
|
||||
multi: true
|
||||
}));
|
||||
@Directive({
|
||||
selector: '[minlength][ng-control],[minlength][ng-form-control],[minlength][ng-model]',
|
||||
providers: [MIN_LENGTH_VALIDATOR]
|
||||
})
|
||||
export class MinLengthValidator {
|
||||
minLength: number;
|
||||
constructor(@Attribute("minlength") minLength: string) {
|
||||
this.minLength = NumberWrapper.parseInt(minLength, 10);
|
||||
}
|
||||
}
|
||||
|
||||
function createMaxLengthValidator(dir): any {
|
||||
return Validators.maxLength(dir.maxLength);
|
||||
}
|
||||
const MAX_LENGTH_VALIDATOR = CONST_EXPR(new Provider(NG_VALIDATORS, {
|
||||
useFactory: createMaxLengthValidator,
|
||||
deps: [forwardRef(() => MaxLengthValidator)],
|
||||
multi: true
|
||||
}));
|
||||
@Directive({
|
||||
selector: '[maxlength][ng-control],[maxlength][ng-form-control],[maxlength][ng-model]',
|
||||
providers: [MAX_LENGTH_VALIDATOR]
|
||||
})
|
||||
export class MaxLengthValidator {
|
||||
maxLength: number;
|
||||
constructor(@Attribute("maxlength") maxLength: string) {
|
||||
this.maxLength = NumberWrapper.parseInt(maxLength, 10);
|
||||
}
|
||||
}
|
@ -21,6 +21,26 @@ export class Validators {
|
||||
return isBlank(control.value) || control.value == "" ? {"required": true} : null;
|
||||
}
|
||||
|
||||
static minLength(minLength: number): Function {
|
||||
return (control: modelModule.Control): {[key: string]: any} => {
|
||||
if (isPresent(Validators.required(control))) return null;
|
||||
var v: string = control.value;
|
||||
return v.length < minLength ?
|
||||
{"minlength": {"requiredLength": minLength, "actualLength": v.length}} :
|
||||
null;
|
||||
};
|
||||
}
|
||||
|
||||
static maxLength(maxLength: number): Function {
|
||||
return (control: modelModule.Control): {[key: string]: any} => {
|
||||
if (isPresent(Validators.required(control))) return null;
|
||||
var v: string = control.value;
|
||||
return v.length > maxLength ?
|
||||
{"maxlength": {"requiredLength": maxLength, "actualLength": v.length}} :
|
||||
null;
|
||||
};
|
||||
}
|
||||
|
||||
static nullValidator(c: any): {[key: string]: boolean} { return null; }
|
||||
|
||||
static compose(validators: Function[]): Function {
|
||||
|
Reference in New Issue
Block a user