Skip to content

Commit 23cc3ef

Browse files
dirkluijkalxhub
authored andcommitted
fix(forms): improve error message for invalid value accessors (#22731)
Signed-off-by: Dirk Luijk <mail@dirkluijk.nl> PR Close #22731
1 parent 550433a commit 23cc3ef

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

‎packages/forms/src/directives/shared.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,13 @@ export function selectValueAccessor(
187187
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null {
188188
if (!valueAccessors) return null;
189189

190+
if (!Array.isArray(valueAccessors))
191+
_throwError(dir, 'Value accessor was not provided as an array for form control with');
192+
190193
let defaultAccessor: ControlValueAccessor|undefined = undefined;
191194
let builtinAccessor: ControlValueAccessor|undefined = undefined;
192195
let customAccessor: ControlValueAccessor|undefined = undefined;
196+
193197
valueAccessors.forEach((v: ControlValueAccessor) => {
194198
if (v.constructor === DefaultValueAccessor) {
195199
defaultAccessor = v;

‎packages/forms/test/directives_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function asyncValidator(expected: any, timeout = 0) {
5555
it('should throw when given an empty array',
5656
() => { expect(() => selectValueAccessor(dir, [])).toThrowError(); });
5757

58+
it('should throw when accessor is not provided as array', () => {
59+
expect(() => selectValueAccessor(dir, {} as any[]))
60+
.toThrowError(
61+
`Value accessor was not provided as an array for form control with unspecified name attribute`);
62+
});
63+
5864
it('should return the default value accessor when no other provided',
5965
() => { expect(selectValueAccessor(dir, [defaultAccessor])).toEqual(defaultAccessor); });
6066

‎packages/forms/test/spies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export class SpyChangeDetectorRef extends SpyObject {
1616
}
1717
}
1818

19-
export class SpyNgControl extends SpyObject {}
19+
export class SpyNgControl extends SpyObject { path = []; }
2020

2121
export class SpyValueAccessor extends SpyObject { writeValue: any; }

0 commit comments

Comments
 (0)