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:
parent
550433a128
commit
23cc3ef2eb
|
@ -187,9 +187,13 @@ export function selectValueAccessor(
|
|||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|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 builtinAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let customAccessor: ControlValueAccessor|undefined = undefined;
|
||||
|
||||
valueAccessors.forEach((v: ControlValueAccessor) => {
|
||||
if (v.constructor === DefaultValueAccessor) {
|
||||
defaultAccessor = v;
|
||||
|
|
|
@ -55,6 +55,12 @@ function asyncValidator(expected: any, timeout = 0) {
|
|||
it('should throw when given an empty array',
|
||||
() => { 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',
|
||||
() => { 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; }
|
||||
|
|
Loading…
Reference in New Issue