2016-02-19 11:49:31 -08:00
|
|
|
import {forwardRef, Provider, Attribute, Directive} from 'angular2/core';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-09-02 10:24:22 -07:00
|
|
|
import {Validators, NG_VALIDATORS} from '../validators';
|
2016-02-19 11:49:31 -08:00
|
|
|
import {AbstractControl} from '../model';
|
2015-10-28 16:54:27 -07:00
|
|
|
import * as modelModule from '../model';
|
2016-04-07 17:17:50 -07:00
|
|
|
import {NumberWrapper} from 'angular2/src/facade/lang';
|
2015-06-10 13:51:44 -07:00
|
|
|
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2016-02-19 11:49:31 -08: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};
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2016-02-19 11:49:31 -08:00
|
|
|
export interface Validator { validate(c: modelModule.AbstractControl): {[key: string]: any}; }
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2016-03-16 11:46:31 -07:00
|
|
|
const REQUIRED = Validators.required;
|
|
|
|
|
2015-10-13 14:38:13 -07:00
|
|
|
const REQUIRED_VALIDATOR =
|
2016-03-16 11:46:31 -07:00
|
|
|
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: 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.
|
|
|
|
*
|
2015-11-17 09:41:31 -08:00
|
|
|
* ### Example
|
2015-09-29 12:59:56 -07:00
|
|
|
*
|
|
|
|
* ```
|
2015-11-23 16:02:19 -08:00
|
|
|
* <input ngControl="fullName" required>
|
2015-09-29 12:59:56 -07:00
|
|
|
* ```
|
|
|
|
*/
|
2015-06-17 14:45:40 -07:00
|
|
|
@Directive({
|
2015-11-23 16:02:19 -08:00
|
|
|
selector: '[required][ngControl],[required][ngFormControl],[required][ngModel]',
|
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
|
|
|
|
2016-02-19 11:49:31 -08:00
|
|
|
export interface ValidatorFn { (c: AbstractControl): {[key: string]: any}; }
|
|
|
|
export interface AsyncValidatorFn {
|
|
|
|
(c: AbstractControl): any /*Promise<{[key: string]: any}>|Observable<{[key: string]: any}>*/;
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:49:09 -08:00
|
|
|
/**
|
|
|
|
* Provivder which adds {@link MinLengthValidator} to {@link NG_VALIDATORS}.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example common/forms/ts/validators/validators.ts region='min'}
|
|
|
|
*/
|
2015-10-28 16:54:27 -07:00
|
|
|
const MIN_LENGTH_VALIDATOR = CONST_EXPR(
|
|
|
|
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MinLengthValidator), multi: true}));
|
2015-12-03 15:49:09 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A directive which installs the {@link MinLengthValidator} for any `ngControl`,
|
|
|
|
* `ngFormControl`, or control with `ngModel` that also has a `minlength` attribute.
|
|
|
|
*/
|
2015-10-13 14:38:13 -07:00
|
|
|
@Directive({
|
2015-11-23 16:02:19 -08:00
|
|
|
selector: '[minlength][ngControl],[minlength][ngFormControl],[minlength][ngModel]',
|
2015-10-13 14:38:13 -07:00
|
|
|
providers: [MIN_LENGTH_VALIDATOR]
|
|
|
|
})
|
2015-10-28 16:54:27 -07:00
|
|
|
export class MinLengthValidator implements Validator {
|
2016-02-19 11:49:31 -08:00
|
|
|
private _validator: ValidatorFn;
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2016-04-07 17:17:50 -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
|
|
|
}
|
|
|
|
|
2016-02-19 11:49:31 -08:00
|
|
|
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2015-12-03 15:49:09 -08:00
|
|
|
/**
|
|
|
|
* Provider which adds {@link MaxLengthValidator} to {@link NG_VALIDATORS}.
|
|
|
|
*
|
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {@example common/forms/ts/validators/validators.ts region='max'}
|
|
|
|
*/
|
2015-10-28 16:54:27 -07:00
|
|
|
const MAX_LENGTH_VALIDATOR = CONST_EXPR(
|
|
|
|
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MaxLengthValidator), multi: true}));
|
2015-12-03 15:49:09 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A directive which installs the {@link MaxLengthValidator} for any `ngControl, `ngFormControl`,
|
|
|
|
* or control with `ngModel` that also has a `maxlength` attribute.
|
|
|
|
*/
|
2015-10-13 14:38:13 -07:00
|
|
|
@Directive({
|
2015-11-23 16:02:19 -08:00
|
|
|
selector: '[maxlength][ngControl],[maxlength][ngFormControl],[maxlength][ngModel]',
|
2015-10-13 14:38:13 -07:00
|
|
|
providers: [MAX_LENGTH_VALIDATOR]
|
|
|
|
})
|
2015-10-28 16:54:27 -07:00
|
|
|
export class MaxLengthValidator implements Validator {
|
2016-02-19 11:49:31 -08:00
|
|
|
private _validator: ValidatorFn;
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2016-04-07 17:17:50 -07:00
|
|
|
constructor(@Attribute('maxlength') maxLength: string) {
|
2015-12-04 00:15:04 +00:00
|
|
|
this._validator = Validators.maxLength(NumberWrapper.parseInt(maxLength, 10));
|
2015-10-13 14:38:13 -07:00
|
|
|
}
|
2015-10-28 16:54:27 -07:00
|
|
|
|
2016-02-19 11:49:31 -08:00
|
|
|
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
|
2015-11-17 09:41:31 -08:00
|
|
|
}
|
2015-11-20 01:36:28 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Directive that adds the `pattern` validator to any controls marked with the
|
|
|
|
* `pattern` attribute, via the {@link NG_VALIDATORS} binding. Uses attribute value
|
|
|
|
* as the regex to validate Control value against. Follows pattern attribute
|
|
|
|
* semantics; i.e. regex must match entire Control value.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <input [ngControl]="fullName" pattern="[a-zA-Z ]*">
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
const PATTERN_VALIDATOR = CONST_EXPR(
|
|
|
|
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => PatternValidator), multi: true}));
|
|
|
|
@Directive({
|
|
|
|
selector: '[pattern][ngControl],[pattern][ngFormControl],[pattern][ngModel]',
|
|
|
|
providers: [PATTERN_VALIDATOR]
|
|
|
|
})
|
|
|
|
export class PatternValidator implements Validator {
|
2016-02-19 11:49:31 -08:00
|
|
|
private _validator: ValidatorFn;
|
2015-11-20 01:36:28 -05:00
|
|
|
|
2016-04-07 17:17:50 -07:00
|
|
|
constructor(@Attribute('pattern') pattern: string) {
|
2015-11-20 01:36:28 -05:00
|
|
|
this._validator = Validators.pattern(pattern);
|
|
|
|
}
|
|
|
|
|
2016-02-19 11:49:31 -08:00
|
|
|
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
|
2015-11-20 01:36:28 -05:00
|
|
|
}
|