Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: defaultValues are not re-read on form reset #392

Open
1 of 5 tasks
Menduist opened this issue Sep 18, 2024 · 3 comments
Open
1 of 5 tasks

[Bug]: defaultValues are not re-read on form reset #392

Menduist opened this issue Sep 18, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Menduist
Copy link

Which packages are impacted?

  • @rvf/react
  • @rvf/remix
  • @rvf/zod
  • @rvf/yup
  • zod-form-data

What version of these packages are you using?

  • "@rvf/remix": "^6.1.3"
  • "@rvf/zod": "^6.0.3",

Please provide a link to a minimal reproduction of the issue.

https://stackblitz.com/edit/stackblitz-starters-xtvc7x?file=app%2Froutes%2F_index.tsx

Steps to Reproduce the Bug or Issue

The repro contains 3 examples: Broky1, Broky2 & Worky. Switch the default between them to see the different behaviors.

Expected behavior

Since resetAfterSubmit is set, and the loader is returning random data, I would expect the input to update its value on each validation
Instead, the initial default value is kept

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: All

Additional context

No response

@Menduist Menduist added the bug Something isn't working label Sep 18, 2024
@airjp73
Copy link
Owner

airjp73 commented Sep 18, 2024

I think this is the correct behavior. If reset always looked at looked at the latest defaultValues, there would no longer be a guarantee that it would actually reset to the expected values. And if resetAfterSubmit had different behavior than onSubmitSuccess={() => form.resetForm()} that would break expectations as well.

But I think this is worth clarifying in the docs.

We could potentially add first-calls support for this case by letting you specify something like resetAfterFormSubmit="reinitialize" or by adding a key to useForm that would reset & reinitialize.

@Menduist
Copy link
Author

I've added a Worky2 to the example that uses a remix Form, you'll see that it behaves as I would expect (updates the values)

IMO, the defaultValue of each field should always be up to date with the defaultValues of useForm, and thus the reset should always put back the latest defaultValues. This stays consistent with "vanilla react"

@Lemmmy
Copy link

Lemmmy commented Oct 16, 2024

Is there any way to work around this currently, without implementing the whole fetch/submit flow manually? I can see that form.resetForm takes optional new values for the form, but with @rvf/remix's useForm, onSubmitSuccess doesn't seem to get called with the new data?

Edit: Managed to get Worky from above working, with the following code:

  const data = useActionData<typeof action>();
  const form = useForm({
    defaultValues: example,
    validator: exampleValidator,
    method: "POST",
    onSubmitSuccess: () => {
      if (data && "example" in data) {
        form.resetForm(data.example);
      }
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants