feat(forms): add get method for easy access to child controls (#10428)
This commit is contained in:
parent
2dfc9c653b
commit
8d4499959a
@ -34,11 +34,11 @@ export function isControl(control: Object): boolean {
|
|||||||
return control instanceof AbstractControl;
|
return control instanceof AbstractControl;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _find(control: AbstractControl, path: Array<string|number>| string) {
|
function _find(control: AbstractControl, path: Array<string|number>| string, delimiter: string) {
|
||||||
if (isBlank(path)) return null;
|
if (isBlank(path)) return null;
|
||||||
|
|
||||||
if (!(path instanceof Array)) {
|
if (!(path instanceof Array)) {
|
||||||
path = (<string>path).split('/');
|
path = (<string>path).split(delimiter);
|
||||||
}
|
}
|
||||||
if (path instanceof Array && ListWrapper.isEmpty(path)) return null;
|
if (path instanceof Array && ListWrapper.isEmpty(path)) return null;
|
||||||
|
|
||||||
@ -251,7 +251,12 @@ export abstract class AbstractControl {
|
|||||||
this._updateControlsErrors(emitEvent);
|
this._updateControlsErrors(emitEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
find(path: Array<string|number>|string): AbstractControl { return _find(this, path); }
|
/**
|
||||||
|
* @deprecated - use get() instead
|
||||||
|
*/
|
||||||
|
find(path: Array<string|number>|string): AbstractControl { return _find(this, path, '/'); }
|
||||||
|
|
||||||
|
get(path: Array<string|number>|string): AbstractControl { return _find(this, path, '.'); }
|
||||||
|
|
||||||
getError(errorCode: string, path: string[] = null): any {
|
getError(errorCode: string, path: string[] = null): any {
|
||||||
var control = isPresent(path) && !ListWrapper.isEmpty(path) ? this.find(path) : this;
|
var control = isPresent(path) && !ListWrapper.isEmpty(path) ? this.find(path) : this;
|
||||||
|
@ -1626,6 +1626,42 @@ export function main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('get', () => {
|
||||||
|
it('should return null when path is null', () => {
|
||||||
|
var g = new FormGroup({});
|
||||||
|
expect(g.get(null)).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null when path is empty', () => {
|
||||||
|
var g = new FormGroup({});
|
||||||
|
expect(g.get([])).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null when path is invalid', () => {
|
||||||
|
var g = new FormGroup({});
|
||||||
|
expect(g.get('invalid')).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a child of a control group', () => {
|
||||||
|
var g = new FormGroup({
|
||||||
|
'one': new FormControl('111'),
|
||||||
|
'nested': new FormGroup({'two': new FormControl('222')})
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(g.get(['one']).value).toEqual('111');
|
||||||
|
expect(g.get('one').value).toEqual('111');
|
||||||
|
expect(g.get(['nested', 'two']).value).toEqual('222');
|
||||||
|
expect(g.get('nested.two').value).toEqual('222');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an element of an array', () => {
|
||||||
|
var g = new FormGroup({'array': new FormArray([new FormControl('111')])});
|
||||||
|
|
||||||
|
expect(g.get(['array', 0]).value).toEqual('111');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('asyncValidator', () => {
|
describe('asyncValidator', () => {
|
||||||
it('should run the async validator', fakeAsync(() => {
|
it('should run the async validator', fakeAsync(() => {
|
||||||
var c = new FormControl('value');
|
var c = new FormControl('value');
|
||||||
|
3
tools/public_api_guard/forms/index.d.ts
vendored
3
tools/public_api_guard/forms/index.d.ts
vendored
@ -19,7 +19,8 @@ export declare abstract class AbstractControl {
|
|||||||
constructor(validator: ValidatorFn, asyncValidator: AsyncValidatorFn);
|
constructor(validator: ValidatorFn, asyncValidator: AsyncValidatorFn);
|
||||||
clearAsyncValidators(): void;
|
clearAsyncValidators(): void;
|
||||||
clearValidators(): void;
|
clearValidators(): void;
|
||||||
find(path: Array<string | number> | string): AbstractControl;
|
/** @deprecated */ find(path: Array<string | number> | string): AbstractControl;
|
||||||
|
get(path: Array<string | number> | string): AbstractControl;
|
||||||
getError(errorCode: string, path?: string[]): any;
|
getError(errorCode: string, path?: string[]): any;
|
||||||
hasError(errorCode: string, path?: string[]): boolean;
|
hasError(errorCode: string, path?: string[]): boolean;
|
||||||
markAsDirty({onlySelf}?: {
|
markAsDirty({onlySelf}?: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user