Skip to content

Commit 65a51a2

Browse files
fix: ensure form has elements before trying to process data
1 parent dc04339 commit 65a51a2

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

Diff for: src/utils.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ export const formResetCallback = (event: Event) => {
129129
/** Get the Set of elements attached to this form */
130130
const elements = formElementsMap.get(event.target as HTMLFormElement);
131131

132-
/** Loop over the elements and call formResetCallback if applicable */
133-
elements.forEach(element => {
134-
if ((element.constructor as any).formAssociated && element.formResetCallback) {
135-
element.formResetCallback.apply(element);
136-
}
137-
});
132+
/** Some forms won't contain form associated custom elements */
133+
if (elements && elements.size) {
134+
/** Loop over the elements and call formResetCallback if applicable */
135+
elements.forEach(element => {
136+
if ((element.constructor as any).formAssociated && element.formResetCallback) {
137+
element.formResetCallback.apply(element);
138+
}
139+
});
140+
}
138141
};
139142

140143
/**
@@ -213,13 +216,17 @@ export const throwIfNotFormAssociated = (ref: ICustomElement, message: string, E
213216
*/
214217
export const overrideFormMethod = (form: HTMLFormElement, returnValue: boolean, method: 'checkValidity'|'reportValidity'): boolean => {
215218
const elements = formElementsMap.get(form);
216-
elements.forEach(element => {
217-
const internals = internalsMap.get(element);
218-
const valid = internals[method]();
219-
if (!valid) {
220-
returnValue = false;
221-
}
222-
});
219+
220+
/** Some forms won't contain form associated custom elements */
221+
if (elements && elements.size) {
222+
elements.forEach(element => {
223+
const internals = internalsMap.get(element);
224+
const valid = internals[method]();
225+
if (!valid) {
226+
returnValue = false;
227+
}
228+
});
229+
}
223230
return returnValue;
224231
};
225232

0 commit comments

Comments
 (0)