Replies: 42 comments 44 replies
|
Should be working properly. See this CSB
|
|
Hmm. If I initialize all fields with Isn't this a bit odd? |
|
So, talking in #7590, it seems like the only correct way to have controlled input fields properly reset is to set their default value as a string. |
|
It worked for me as well with the controller component |
|
Im still struggling with this. I initialise my form with the following. Bear in mind there are other fields in But calls to reset, even with passing in the original defaultValues, have no effect. How can I get it to reset without having to pass in an empty string for every field? |
|
I have the same problem, I understand that I can reset all field, giving theme a default value like an empty string, but I don't really want to do that, otherwise the validation logic (zod) won't show required, but something like you passed a string where a number is required. I have also tried to set all values to undefined instead calling reset, but while internally the value is set to undefined, the ui is not updated. |
|
Same problem here, if i specify |
|
In my case the form state still remains even though setting reset(initialPayload, { keepIsSubmitted: false, keepSubmitCount: false }); |
|
Also having trouble with this ... The only way to reset the form right now is to use |
|
Has the same issue. This but happens after the 7.22.0 version downgrading works to me. @bluebill1049 |
|
any update on this, facing issue on |
|
Same issue here. |
|
I have found a workaround for this. For some reason |
|
Same issue here, really annoying. I have to reset each field manually in a dirty way (using zod validation) form.reset({
field1: null as any,
field2: null as any
...
}) |
|
Any updates on a fix here? Resetting by setting every key to null works but is annoying to do all the time especially for dynamic forms. |
|
For me that worked was to pass to e.g.
|
|
Really don't understand why this is hard to implement, and it seems like it should be a core part of the library. If I call a method called reset, I want my form to go back to its default, initial state. Even if I pass in my default object to the reset method, it still maintains existing values for things I don't explicitly set to null! Even setting For now, to support my use case of dynamic forms, I am doing this to make sure it gets reset properly: const existingValueReset = form.getValues();
for (let key of Object.keys(existingValueReset)) {
existingValueReset[key] = null;
}
reset({ ...existingValueReset, ...defaultValues });Another option would be to use the callback of reset: reset((prev) => {
Object.keys(prev).forEach((key) => prev[key] = null);
return {...prev, ...defaultValues};
}); |
|
I found a solution for myself. "react-hook-form": "^7.46.1" |
|
I am loading the default form values from server side and when the response comes back, It's very inconvenience to do so with current implementation. |
|
Without handlying events by yourself: You can do further clearing operations by adding an onClickHandler. But the basic input element only get cleared if you set the type to reset, of the reset button. |
|
This worked for me: let defaultValues const { useEffect(() => { const handleClose = () => { |
|
I had this issue with an array of objects not resetting when opening in a dialog. My fix for now. |
|
It seemed like the formState wasn't being reflected internally, |
|
Here, another solution:
function onSubmit(data) {
try {
console.log("Form Submitted!");
console.log(data);
} catch (error) {
console.log("errors: ", error);
} finally {
reset();
}
}
|
|
I made zod-empty to create defaultValues. use like this: import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { empty } from "zod-empty";
const schema = z.object({
name: z.string().min(1, { message: "Required" }),
age: z.number().min(10),
company: z.string().default("Acme inc."),
hobbies: z.array(z.string()),
});
// create default values with zod-empty
const defaultValues = empty(schema); // => { name: "", age: null, company: "Acme inc.", hobbies: [] }
const form = useForm({
// pass default values
defaultValues,
resolver: zodResolver(schema),
}); |
|
alternatively, you can use: resetField("reference.key", {
defaultValue: value
}so you don't have to enter all the fields with the right conditions. |
|
I generally need to reset the form when submitting, so my solution works well for me: <form
className="space-y-8"
onSubmit={event => {
if (form.formState.isValid) {
event.currentTarget.reset();
}
return form.handleSubmit(onSubmit)(event);
}}
noValidate
>
// ...Like me, you might think about calling |
|
same issue. |
|
Still one of the major pain points in this library. Every time I need to implement a form that supports both const methods = useForm<xInsertFormValues | xUpdateFormValues>({
mode: "onChange",
resolver: zodResolver(
isEditMode ? xUpdateSchema : xInsertSchema,
),
});
useEffect(() => {
const resetValue =
isEditMode && selectedRows.length === 1
? ({
foo: selectedRows[0].foo,
bar: selectedRows[0].bar,
} as xUpdateFormValues)
: ({} as xInsertFormValues);
methods.reset(resetValue, { keepDefaultValues: false, keepValues: false });
}, [isEditMode, methods.reset, selectedRows]); |

Uh oh!
There was an error while loading. Please reload this page.
I'm trying to clear my form, but only succeed half the way.
The stored values in RHF is cleared, but the values in my input components are still there.
I have also tried with keepValues: false
If I explicitly set the values to null, the actual input fields gets cleared.
My fields are controlled. I have connected
onChange,valueandref. Everything works as it should in the form, butresetmis-behaves or is mis-used by me.All reactions