Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): remove equalsTo validator #15050

Merged
merged 1 commit into from Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 0 additions & 24 deletions packages/forms/src/validators.ts
Expand Up @@ -59,30 +59,6 @@ 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.
*/
Expand Down
38 changes: 0 additions & 38 deletions packages/forms/test/validators_spec.ts
Expand Up @@ -38,44 +38,6 @@ export function main() {
}

describe('Validators', () => {
describe('equalsTo', () => {
it('should not error when equal', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('a')});
let validator = Validators.equalsTo('f2');
expect(validator(group.controls['f1'])).toBeNull();
});

it('should error when not equal', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('b')});
let validator = Validators.equalsTo('f2');
expect(validator(group.controls['f1'])).toEqual({equalsTo: {unequalField: 'f2'}});
});

it('should throw if passed a form control', () => {
let validator = Validators.equalsTo('f1', 'f2');
// cast it to any so we don't get TS errors
expect(() => validator(<any>new FormGroup({f1: new FormControl('')}))).toThrow();
});

it('should throw if passed a form array', () => {
let validator = Validators.equalsTo('f1', 'f2');
// cast it to any so we don't get TS errors
expect(() => validator(<any>new FormArray([]))).toThrow();
});

it('should throw if not passed any field to compare', () => {
let validator = Validators.equalsTo();
expect(() => validator(new FormControl('a'))).toThrow();
});

it('should throw if field passed does not exist in the group', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('b')});
let validator = Validators.equalsTo('f3', 'f4');
// cast it to any so we don't get TS errors
expect(() => validator(new FormControl('a'))).toThrow();
});
});

describe('required', () => {
it('should error on an empty string',
() => { expect(Validators.required(new FormControl(''))).toEqual({'required': true}); });
Expand Down
1 change: 0 additions & 1 deletion tools/public_api_guard/forms/typings/forms.d.ts
Expand Up @@ -554,7 +554,6 @@ export declare class Validators {
static email(control: AbstractControl): {
[key: string]: boolean;
};
static equalsTo(...fieldPaths: string[]): ValidatorFn;
static maxLength(maxLength: number): ValidatorFn;
static minLength(minLength: number): ValidatorFn;
static nullValidator(c: AbstractControl): {
Expand Down