Skip to content

Commit f11acbd

Browse files
committed
fix: handle event instead of data in ObjectForm submission and add defensive checks
1 parent eed73fa commit f11acbd

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

apps/console/src/__tests__/ObjectFormUnit.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ describe('ObjectForm Unit Tests', () => {
115115
// Fill in form
116116
await user.type(screen.getByLabelText(/Full Name/i), 'Test User');
117117
await user.type(screen.getByLabelText(/Email/i), 'test@example.com');
118-
118+
119+
// Debug DOM state
120+
// console.log('DEBUG_DOM_HTML:', screen.getByLabelText(/Full Name/i).outerHTML);
121+
119122
// Submit
120123
const submitButton = screen.getByRole('button', { name: /create/i });
121124
await user.click(submitButton);

packages/components/src/renderers/form/form.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ ComponentRegistry.register('form',
7777

7878
// Handle form submission
7979
const handleSubmit = form.handleSubmit(async (data) => {
80-
console.log('Form Renderer handleSubmit data:', data);
8180
setIsSubmitting(true);
8281
setSubmitError(null);
8382

@@ -93,7 +92,7 @@ ComponentRegistry.register('form',
9392
);
9493

9594
if (isEvent) {
96-
console.warn('Form Renderer: Received Event instead of data! Fetching values manually.');
95+
// This should not happen with RHF handleSubmit, but just in case
9796
formData = form.getValues();
9897
} else if (!formData || Object.keys(formData).length === 0) {
9998
// Fallback: if data is empty check getValues(), in case RHF failed to pass it for some reason
@@ -172,11 +171,13 @@ ComponentRegistry.register('form',
172171
? cn('grid gap-4', gridColsClass)
173172
: 'space-y-4';
174173

175-
// Extract designer-related props
174+
// Extract designer-related props and conflicting handlers
176175
const {
177176
'data-obj-id': dataObjId,
178177
'data-obj-type': dataObjType,
179-
style,
178+
style,
179+
onSubmit: _ignoredOnSubmit, // Prevent overwriting our handleSubmit
180+
onChange: _ignoredOnChange, // Prevent overwriting our onChange
180181
...formProps
181182
} = props;
182183

packages/plugin-form/src/ObjectForm.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,19 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
262262
}, [objectSchema, schema.fields, schema.customFields, schema.readOnly, schema.mode, hasInlineFields]);
263263

264264
// Handle form submission
265-
const handleSubmit = useCallback(async (formData: any) => {
265+
const handleSubmit = useCallback(async (formData: any, e?: any) => {
266+
// If we receive an event as the first argument, it means the Form renderer passed the event instead of data
267+
// This happens when react-hook-form's handleSubmit is bypassed or configured incorrectly
268+
if (formData && (formData.nativeEvent || formData._reactName === 'onSubmit')) {
269+
console.warn('ObjectForm: Received Event instead of data in handleSubmit! This suggests a Form renderer issue.');
270+
// Proceed defensively - we can't do much if we don't have data, but let's try to not crash
271+
// If we are here, formData is actually the event
272+
if (e === undefined) {
273+
e = formData;
274+
formData = {}; // Reset to empty object or we try to submit the Event object
275+
}
276+
}
277+
266278
console.log('ObjectForm submitting data:', formData);
267279
// For inline fields without a dataSource, just call the success callback
268280
if (hasInlineFields && !dataSource) {

0 commit comments

Comments
 (0)