fix(forms): match getError and hasError to get method signature (#20211)
Internally getError and hasError call the AbstractControl#get method which takes `path: Array<string | number> | string` as input, since there are different ways to traverse the AbstractControl tree. This change matches the method signitures of all methods that use this. PR Close #20211
This commit is contained in:
parent
e268a0579a
commit
1b0b36d143
@ -177,7 +177,7 @@ export abstract class AbstractControlDirective {
|
|||||||
*
|
*
|
||||||
* If the control is not present, false is returned.
|
* 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;
|
return this.control ? this.control.hasError(errorCode, path) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ export abstract class AbstractControlDirective {
|
|||||||
* @returns error data for that particular error. If the control or error is not present,
|
* @returns error data for that particular error. If the control or error is not present,
|
||||||
* null is returned.
|
* 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;
|
return this.control ? this.control.getError(errorCode, path) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -671,7 +671,7 @@ export abstract class AbstractControl {
|
|||||||
* @returns error data for that particular error. If the control or error is not present,
|
* @returns error data for that particular error. If the control or error is not present,
|
||||||
* null is returned.
|
* null is returned.
|
||||||
*/
|
*/
|
||||||
getError(errorCode: string, path?: string[]): any {
|
getError(errorCode: string, path?: Array<string|number>|string): any {
|
||||||
const control = path ? this.get(path) : this;
|
const control = path ? this.get(path) : this;
|
||||||
return control && control.errors ? control.errors[errorCode] : null;
|
return control && control.errors ? control.errors[errorCode] : null;
|
||||||
}
|
}
|
||||||
@ -706,7 +706,9 @@ export abstract class AbstractControl {
|
|||||||
*
|
*
|
||||||
* If the control is not present, false is returned.
|
* If the control is not present, false is returned.
|
||||||
*/
|
*/
|
||||||
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.
|
* Retrieves the top-level ancestor of this control.
|
||||||
|
@ -684,6 +684,70 @@ import {of } from 'rxjs';
|
|||||||
expect(g.getError('required', ['one'])).toEqual(null);
|
expect(g.getError('required', ['one'])).toEqual(null);
|
||||||
expect(g.getError('required', ['invalid'])).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', () => {
|
describe('validator', () => {
|
||||||
|
8
tools/public_api_guard/forms/forms.d.ts
vendored
8
tools/public_api_guard/forms/forms.d.ts
vendored
@ -30,8 +30,8 @@ export declare abstract class AbstractControl {
|
|||||||
emitEvent?: boolean;
|
emitEvent?: boolean;
|
||||||
}): void;
|
}): void;
|
||||||
get(path: Array<string | number> | string): AbstractControl | null;
|
get(path: Array<string | number> | string): AbstractControl | null;
|
||||||
getError(errorCode: string, path?: string[]): any;
|
getError(errorCode: string, path?: Array<string | number> | string): any;
|
||||||
hasError(errorCode: string, path?: string[]): boolean;
|
hasError(errorCode: string, path?: Array<string | number> | string): boolean;
|
||||||
markAsDirty(opts?: {
|
markAsDirty(opts?: {
|
||||||
onlySelf?: boolean;
|
onlySelf?: boolean;
|
||||||
}): void;
|
}): void;
|
||||||
@ -80,8 +80,8 @@ export declare abstract class AbstractControlDirective {
|
|||||||
readonly valid: boolean | null;
|
readonly valid: boolean | null;
|
||||||
readonly value: any;
|
readonly value: any;
|
||||||
readonly valueChanges: Observable<any> | null;
|
readonly valueChanges: Observable<any> | null;
|
||||||
getError(errorCode: string, path?: string[]): any;
|
getError(errorCode: string, path?: Array<string | number> | string): any;
|
||||||
hasError(errorCode: string, path?: string[]): boolean;
|
hasError(errorCode: string, path?: Array<string | number> | string): boolean;
|
||||||
reset(value?: any): void;
|
reset(value?: any): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user