fix(forms): improve error message for invalid value accessors (#22731)
Signed-off-by: Dirk Luijk <mail@dirkluijk.nl> PR Close #22731
This commit is contained in:

committed by
Alex Rickabaugh

parent
550433a128
commit
23cc3ef2eb
@ -187,9 +187,13 @@ export function selectValueAccessor(
|
|||||||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null {
|
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null {
|
||||||
if (!valueAccessors) return null;
|
if (!valueAccessors) return null;
|
||||||
|
|
||||||
|
if (!Array.isArray(valueAccessors))
|
||||||
|
_throwError(dir, 'Value accessor was not provided as an array for form control with');
|
||||||
|
|
||||||
let defaultAccessor: ControlValueAccessor|undefined = undefined;
|
let defaultAccessor: ControlValueAccessor|undefined = undefined;
|
||||||
let builtinAccessor: ControlValueAccessor|undefined = undefined;
|
let builtinAccessor: ControlValueAccessor|undefined = undefined;
|
||||||
let customAccessor: ControlValueAccessor|undefined = undefined;
|
let customAccessor: ControlValueAccessor|undefined = undefined;
|
||||||
|
|
||||||
valueAccessors.forEach((v: ControlValueAccessor) => {
|
valueAccessors.forEach((v: ControlValueAccessor) => {
|
||||||
if (v.constructor === DefaultValueAccessor) {
|
if (v.constructor === DefaultValueAccessor) {
|
||||||
defaultAccessor = v;
|
defaultAccessor = v;
|
||||||
|
@ -55,6 +55,12 @@ function asyncValidator(expected: any, timeout = 0) {
|
|||||||
it('should throw when given an empty array',
|
it('should throw when given an empty array',
|
||||||
() => { expect(() => selectValueAccessor(dir, [])).toThrowError(); });
|
() => { expect(() => selectValueAccessor(dir, [])).toThrowError(); });
|
||||||
|
|
||||||
|
it('should throw when accessor is not provided as array', () => {
|
||||||
|
expect(() => selectValueAccessor(dir, {} as any[]))
|
||||||
|
.toThrowError(
|
||||||
|
`Value accessor was not provided as an array for form control with unspecified name attribute`);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return the default value accessor when no other provided',
|
it('should return the default value accessor when no other provided',
|
||||||
() => { expect(selectValueAccessor(dir, [defaultAccessor])).toEqual(defaultAccessor); });
|
() => { expect(selectValueAccessor(dir, [defaultAccessor])).toEqual(defaultAccessor); });
|
||||||
|
|
||||||
|
@ -16,6 +16,6 @@ export class SpyChangeDetectorRef extends SpyObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SpyNgControl extends SpyObject {}
|
export class SpyNgControl extends SpyObject { path = []; }
|
||||||
|
|
||||||
export class SpyValueAccessor extends SpyObject { writeValue: any; }
|
export class SpyValueAccessor extends SpyObject { writeValue: any; }
|
||||||
|
Reference in New Issue
Block a user