@@ -12,7 +12,7 @@ import {toPromise} from 'rxjs/operator/toPromise';
|
||||
import {AsyncValidatorFn, Validator, ValidatorFn} from './directives/validators';
|
||||
import {StringMapWrapper} from './facade/collection';
|
||||
import {isPresent} from './facade/lang';
|
||||
import {AbstractControl} from './model';
|
||||
import {AbstractControl, FormControl, FormGroup} from './model';
|
||||
import {isPromise} from './private_import_core';
|
||||
|
||||
function isEmptyInputValue(value: any): boolean {
|
||||
@@ -63,6 +63,30 @@ const EMAIL_REGEXP =
|
||||
* @stable
|
||||
*/
|
||||
export class Validators {
|
||||
/**
|
||||
* Validator that compares the value of the given FormControls
|
||||
*/
|
||||
static equalsTo(...fieldPaths: string[]): ValidatorFn {
|
||||
return function(control: FormControl): {[key: string]: any} {
|
||||
if (fieldPaths.length < 1) {
|
||||
throw new Error('You must compare to at least 1 other field');
|
||||
}
|
||||
|
||||
for (let fieldName of fieldPaths) {
|
||||
let field = (<FormGroup>control.parent).get(fieldName);
|
||||
if (!field) {
|
||||
throw new Error(
|
||||
`Field: ${fieldName} undefined, are you sure that ${fieldName} exists in the group`);
|
||||
}
|
||||
|
||||
if (field.value !== control.value) {
|
||||
return {'equalsTo': {'unequalField': fieldName}};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validator that requires controls to have a non-empty value.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user