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

[Vue 3] useForm composable unexpectedly overwrites defaults after a successful form request #1741

Open
shengslogar opened this issue Dec 5, 2023 · 0 comments

Comments

@shengslogar
Copy link

Version:

  • @inertiajs/vue3 version: 1.0.14

Describe the problem:

useForm({}).reset() is not functional after a successful submission. This is due to the form's internal onSuccess hook setting defaults to the current form state (line 172).

While this may make sense when updating existing resources (e.g. after a PATCH /users/2 request, it would be nice to have defaults updated to any newly-saved state), this doesn't make sense for a form that creates new resources (e.g. POST /users) — i.e. there's no way to get the form back to its original state without calling form.defaults({...}) && form.reset() each time.

onSuccess: async (page) => {
this.processing = false
this.progress = null
this.clearErrors()
this.wasSuccessful = true
this.recentlySuccessful = true
recentlySuccessfulTimeoutId = setTimeout(() => (this.recentlySuccessful = false), 2000)
const onSuccess = options.onSuccess ? await options.onSuccess(page) : null
defaults = cloneDeep(this.data())
this.isDirty = false
return onSuccess
},

I was initially debating opening a discussion question, but peeking at the source code for React and Svelte, as well as looking at the expected behavior of a previous discussion question leads me to believe this is indeed a bug. Older versions of the Vue codebase also do not have this line of code.

Steps to reproduce:

1. Set up a new form

import { useForm } from '@inertiajs/vue3'

const form = useForm({ email: '' })

2. Dirty-ify form

form.email = 'foo@bar.com`

3. Submit form

form.post/patch/etc...

At this point, assuming the form request was successful, form.isDirty === false && form.email === 'foo@bar.com. form.reset() yields no changes.

Workaround:

import { useForm } from '@inertiajs/vue3'

const formDefaults = { email: '' }
const form = useForm(formDefaults)

function resetForm() {
    form.defaults(formDefaults)
    form.reset()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant