Skip to content

Commit

Permalink
refactor(forms): match getError and hasError to get method signature
Browse files Browse the repository at this point in the history
closes #19734
  • Loading branch information
Fabian Wiles committed Dec 19, 2018
1 parent f48a00f commit 1cc91a2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/forms/src/directives/abstract_control_directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export abstract class AbstractControlDirective {
* If no path is given, it checks for the error on the present control.
* If the control is not present, false is returned.
*/
hasError(errorCode: string, path?: string[]): boolean {
hasError(errorCode: string, path?: Array<string|number>|string): boolean {
return this.control ? this.control.hasError(errorCode, path) : false;
}

Expand All @@ -162,7 +162,7 @@ export abstract class AbstractControlDirective {
* Reports error data for the control with the given path.
* If the control is not present, null is returned.
*/
getError(errorCode: string, path?: string[]): any {
getError(errorCode: string, path?: Array<string|number>|string): any {
return this.control ? this.control.getError(errorCode, path) : null;
}
}
6 changes: 4 additions & 2 deletions packages/forms/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ export abstract class AbstractControl {
* @returns The error data if the control with the given path has the given error, otherwise null
* or undefined.
*/
getError(errorCode: string, path?: string[]): any {
getError(errorCode: string, path?: Array<string|number>|string): any {
const control = path ? this.get(path) : this;
return control && control.errors ? control.errors[errorCode] : null;
}
Expand All @@ -667,7 +667,9 @@ export abstract class AbstractControl {
* control.
* @returns True when the control with the given path has the error, otherwise false.
*/
hasError(errorCode: string, path?: string[]): boolean { return !!this.getError(errorCode, path); }
hasError(errorCode: string, path?: Array<string|number>|string): boolean {
return !!this.getError(errorCode, path);
}

/**
* Retrieves the top-level ancestor of this control.
Expand Down
64 changes: 64 additions & 0 deletions packages/forms/test/form_group_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,70 @@ import {of } from 'rxjs';
expect(g.getError('required', ['one'])).toEqual(null);
expect(g.getError('required', ['invalid'])).toEqual(null);
});

it('should be able to traverse group with single string', () => {
const c = new FormControl('', Validators.required);
const g = new FormGroup({'one': c});
expect(c.getError('required')).toEqual(true);
expect(g.getError('required', 'one')).toEqual(true);
});

it('should be able to traverse group with string delimited by dots', () => {
const c = new FormControl('', Validators.required);
const g2 = new FormGroup({'two': c});
const g1 = new FormGroup({'one': g2});
expect(c.getError('required')).toEqual(true);
expect(g1.getError('required', 'one.two')).toEqual(true);
});

it('should traverse group with form array using string and numbers', () => {
const c = new FormControl('', Validators.required);
const g2 = new FormGroup({'two': c});
const a = new FormArray([g2]);
const g1 = new FormGroup({'one': a});
expect(c.getError('required')).toEqual(true);
expect(g1.getError('required', ['one', 0, 'two'])).toEqual(true);
});
});

describe('hasError', () => {
it('should return true when it is present', () => {
const c = new FormControl('', Validators.required);
const g = new FormGroup({'one': c});
expect(c.hasError('required')).toEqual(true);
expect(g.hasError('required', ['one'])).toEqual(true);
});

it('should return false otherwise', () => {
const c = new FormControl('not empty', Validators.required);
const g = new FormGroup({'one': c});
expect(c.hasError('invalid')).toEqual(false);
expect(g.hasError('required', ['one'])).toEqual(false);
expect(g.hasError('required', ['invalid'])).toEqual(false);
});

it('should be able to traverse group with single string', () => {
const c = new FormControl('', Validators.required);
const g = new FormGroup({'one': c});
expect(c.hasError('required')).toEqual(true);
expect(g.hasError('required', 'one')).toEqual(true);
});

it('should be able to traverse group with string delimited by dots', () => {
const c = new FormControl('', Validators.required);
const g2 = new FormGroup({'two': c});
const g1 = new FormGroup({'one': g2});
expect(c.hasError('required')).toEqual(true);
expect(g1.hasError('required', 'one.two')).toEqual(true);
});
it('should traverse group with form array using string and numbers', () => {
const c = new FormControl('', Validators.required);
const g2 = new FormGroup({'two': c});
const a = new FormArray([g2]);
const g1 = new FormGroup({'one': a});
expect(c.getError('required')).toEqual(true);
expect(g1.getError('required', ['one', 0, 'two'])).toEqual(true);
});
});

describe('validator', () => {
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/forms/forms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export declare abstract class AbstractControl {
emitEvent?: boolean;
}): void;
get(path: Array<string | number> | string): AbstractControl | null;
getError(errorCode: string, path?: string[]): any;
hasError(errorCode: string, path?: string[]): boolean;
getError(errorCode: string, path?: Array<string | number> | string): any;
hasError(errorCode: string, path?: Array<string | number> | string): boolean;
markAsDirty(opts?: {
onlySelf?: boolean;
}): void;
Expand Down

0 comments on commit 1cc91a2

Please sign in to comment.