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]: GET url param is not updated after using useFormContext submit #302

Open
2 of 4 tasks
justb3a opened this issue Jun 23, 2023 · 3 comments
Open
2 of 4 tasks
Labels
bug Something isn't working

Comments

@justb3a
Copy link

justb3a commented Jun 23, 2023

Which packages are impacted?

  • remix-validated-form
  • @remix-validated-form/with-zod
  • @remix-validated-form/with-yup
  • zod-form-data

What version of these packages are you using?

"@remix-validated-form/with-zod": "2.0.6",
"remix-validated-form": "5.0.2",

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

https://codesandbox.io/s/billowing-flower-q3cj9m?file=/app/routes/index.tsx

Steps to Reproduce the Bug or Issue

In the demo

  1. Click on "set to Jane", see that the value changes BUT the GET URL param is not updated.
  • ?__rvfInternalFormId=the-form&name=John
  • Change in input field, value is updated and GET param as well -> ?__rvfInternalFormId=the-form&name=Gordon
  • Set the value and update it via "submit", the value is updated, the GET param not

Expected behavior

When using const { submit } = useFormContext('formId'), setting a value controlled with useControlField and then submitting the form using submit should also update the GET param in the url (worked in version 4.x before updating to 5.x).

  const onSubmit = useCallback(() => {
    setNameValue("Jane");
    submit();
  }, [submit, setNameValue]);

Screenshots or Videos

Screenshot 2023-06-23 at 13 44 44

Platform

macos latest firefox and chrome

Additional context

No response

@justb3a justb3a added the bug Something isn't working label Jun 23, 2023
@airjp73
Copy link
Owner

airjp73 commented Jun 23, 2023

This is caused by one of the breaking changes in v5. In the release notes, it's the section titled Values in the form are now captured before validation occurs.

The issue is that setValue from useControlField is asynchronous (like React's own setState), while submit is synchronous. Before, there was an issue where you could submit the form and then change the form values, which has been fixed. This is essentially what's happening here. submit is kicking off the submit before the content of the form has actually been updated in response to updating the controlled field. You can see this in your reproduction as well.

I think this case is still fixable though. We have an internal awaitValueUpdate helper we use when validating individual fields, and I think we should add it to the submission handler function before we capture the form values.

I won't have time to push a fix until at least Monday, so in the meantime you can make your onSubmit an async function and await Promise.resolve() before you submit.

setNameValue("Jane");
await Promise.resolve();
submit();

@justb3a
Copy link
Author

justb3a commented Jun 26, 2023

Thanks for the fast response and the explanation, having a fix for this would be great. At the moment / as a quick fix I use the useSubmit hook from @remix-run/react:

import { useSubmit } from '@remix-run/react'

const submitForm = useSubmit()

submitForm({
   __rvfInternalFormId: formId,
  name
 })

It works but doesn't feel right 😎 What also did work was using a button type="submit" onClick={…}, on click setting the values and then using the native button submit functionality, but the button has always to be available in the markup inside the ValidatedForm (no conditional rendering) and it didn't feel really safe timing wise.

@justb3a
Copy link
Author

justb3a commented Oct 23, 2023

hei again, time flies, is there any update? Would be great if you could add the usage of the internal awaitValueUpdate helper. 🙏

I just had to use again the await Promise.resolve() quick fix including documentation why. Would be great to get rid of this at some point. Thanks.

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

2 participants