-
-
Notifications
You must be signed in to change notification settings - Fork 239
Open
Description
When using the FormBuilder
with the renderFormFields
function, I am unable to set default or prefilled values when editing a form. Despite passing currentRow
as the initial values, the form fields do not update properly in edit mode.
Steps to Reproduce
- Use the
FormBuilder
to render a form dynamically from a JSON schema. - Pass the
currentRow
data object to the form as default or prefilled values. - Observe that the fields remain empty instead of populating with the
currentRow
data.
Example Code
const defaultVals = generateDefaultValues(categoryFormFields);
const formSchema = generateZodSchema(categoryFormFields);
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: defaultVals,
});
// Resetting form values for edit mode
useEffect(() => {
if (open && currentRow) {
form.reset(currentRow); // Doesn't populate fields
}
}, [open, currentRow, form]);
// Rendering form fields
<Form {...form}>
<form id="category-form" onSubmit={form.handleSubmit(onSubmit)}>
{renderFormFields(categoryFormFields, form)}
</form>
</Form>
This code letting it to produce infinite render error
Expected Behavior
- When currentRow is provided, the form fields should populate with the data from currentRow.
- The renderFormFields function should correctly bind the data to the corresponding form fields.
Observed Behavior
- Form fields remain empty in edit mode, even when currentRow has valid data.
- The renderFormFields function does not seem to recognize the updated values passed through form.reset.
but the values are not setting on the fields
Additional Context
- The issue might be related to how renderFormFields handles the defaultValues or reset functionality from React Hook Form.
- The form works correctly in "create mode" (default values), but fails to update when switching to "edit mode".
Metadata
Metadata
Assignees
Labels
No labels