2015-10-10 22:11:13 -07:00
|
|
|
import {forwardRef, Provider, OpaqueToken} from 'angular2/src/core/di';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-10-13 14:38:13 -07:00
|
|
|
import {Attribute, Directive} from 'angular2/src/core/metadata';
|
2015-09-02 10:24:22 -07:00
|
|
|
import {Validators, NG_VALIDATORS} from '../validators';
|
2015-10-28 16:54:27 -07:00
|
|
|
import {Control} from '../model';
|
|
|
|
import * as modelModule from '../model';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {NumberWrapper} from "angular2/src/facade/lang";
|
2015-06-10 13:51:44 -07:00
|
|
|
|
2015-10-28 16:54:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface that can be implemented by classes that can act as validators.
|
|
|
|
*
|
|
|
|
* ## Usage
|
|
|
|
*
|
|
|
|
* ```typescript
|
|
|
|
* @Directive({
|
|
|
|
* selector: '[custom-validator]',
|
|
|
|
* providers: [provide(NG_VALIDATORS, {useExisting: CustomValidatorDirective, multi: true})]
|
|
|
|
* })
|
|
|
|
* class CustomValidatorDirective implements Validator {
|
|
|
|
* validate(c: Control): {[key: string]: any} {
|
|
|
|
* return {"custom": true};
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export interface Validator { validate(c: modelModule.Control): {[key: string]: any}; }
|
|
|
|
|
2015-10-13 14:38:13 -07:00
|
|
|
const REQUIRED_VALIDATOR =
|
2015-10-12 11:30:34 -07:00
|
|
|
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: Validators.required, multi: true}));
|
2015-06-17 14:45:40 -07:00
|
|
|
|
2015-09-29 12:59:56 -07:00
|
|
|
/**
|
|
|
|
* A Directive that adds the `required` validator to any controls marked with the
|
|
|
|
* `required` attribute, via the {@link NG_VALIDATORS} binding.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <input ng-control="fullName" required>
|
|
|
|
* ```
|
|
|
|
*/
|
2015-06-17 14:45:40 -07:00
|
|
|
@Directive({
|
|
|
|
selector: '[required][ng-control],[required][ng-form-control],[required][ng-model]',
|
2015-10-13 14:38:13 -07:00
|
|
|
providers: [REQUIRED_VALIDATOR]
|
2015-06-17 14:45:40 -07:00
|
|
|
})
|
2015-10-13 14:38:13 -07:00
|
|
|
export class RequiredValidator {
|
2015-06-10 13:51:44 -07:00
|
|
|
}
|
2015-10-13 14:38:13 -07:00
|
|
|
|
2015-10-28 16:54:27 -07:00
|
|
|
const MIN_LENGTH_VALIDATOR = CONST_EXPR(
|
|
|
|
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MinLengthValidator), multi: true}));
|
2015-10-13 14:38:13 -07:00
|
|
|
@Directive({
|
|
|
|
selector: '[minlength][ng-control],[minlength][ng-form-control],[minlength][ng-model]',
|
|
|
|
providers: [MIN_LENGTH_VALIDATOR]
|
|
|
|
})
|
2015-10-28 16:54:27 -07:00
|
|
|
export class MinLengthValidator implements Validator {
|
|
|
|
private _validator: Function;
|
|
|
|
|
2015-10-13 14:38:13 -07:00
|
|
|
constructor(@Attribute("minlength") minLength: string) {
|
2015-10-28 16:54:27 -07:00
|
|
|
this._validator = Validators.minLength(NumberWrapper.parseInt(minLength, 10));
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|
|
|
|
|
2015-10-28 16:54:27 -07:00
|
|
|
validate(c: Control): {[key: string]: any} { return this._validator(c); }
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|
2015-10-28 16:54:27 -07:00
|
|
|
|
|
|
|
const MAX_LENGTH_VALIDATOR = CONST_EXPR(
|
|
|
|
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MaxLengthValidator), multi: true}));
|
2015-10-13 14:38:13 -07:00
|
|
|
@Directive({
|
|
|
|
selector: '[maxlength][ng-control],[maxlength][ng-form-control],[maxlength][ng-model]',
|
|
|
|
providers: [MAX_LENGTH_VALIDATOR]
|
|
|
|
})
|
2015-10-28 16:54:27 -07:00
|
|
|
export class MaxLengthValidator implements Validator {
|
|
|
|
private _validator: Function;
|
|
|
|
|
|
|
|
constructor(@Attribute("maxlength") minLength: string) {
|
|
|
|
this._validator = Validators.maxLength(NumberWrapper.parseInt(minLength, 10));
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|
2015-10-28 16:54:27 -07:00
|
|
|
|
|
|
|
validate(c: Control): {[key: string]: any} { return this._validator(c); }
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|