Skip to content

Commit

Permalink
[1.x] useForm wrongly overwrites default values ​​after successful su…
Browse files Browse the repository at this point in the history
…bmission (#1935)

* Hotfix for wrong usage of form.defaults after onSuccess

* Update Vue 2 adapter

(cherry picked from commit d96f4fa)

* Cleanup

(cherry picked from commit fe0eb55)

* Fix "isDirty" state after form submission

---------

Co-authored-by: Pedro Borges <[email protected]>
  • Loading branch information
WillRy and pedroborges authored Sep 21, 2024
1 parent b1304b6 commit ee1aea7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/vue2/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default function useForm<TForm extends FormDataType>(...args): InertiaFor
const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data())
const clonedData = cloneDeep(resolvedData)
if (fields.length === 0) {
this.wasSuccessful = false
defaults = clonedData
Object.assign(this, resolvedData)
} else {
Expand Down Expand Up @@ -165,7 +166,6 @@ export default function useForm<TForm extends FormDataType>(...args): InertiaFor
recentlySuccessfulTimeoutId = setTimeout(() => (this.recentlySuccessful = false), 2000)

const onSuccess = options.onSuccess ? await options.onSuccess(page) : null
defaults = cloneDeep(this.data())
this.isDirty = false
return onSuccess
},
Expand Down Expand Up @@ -236,7 +236,7 @@ export default function useForm<TForm extends FormDataType>(...args): InertiaFor
watch(
form,
(newValue) => {
form.isDirty = !isEqual(form.data(), defaults)
form.isDirty = !form.wasSuccessful && !isEqual(form.data(), defaults)
if (rememberKey) {
router.remember(cloneDeep(newValue.__remember()), rememberKey)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default function useForm<TForm extends FormDataType>(
const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data())
const clonedData = cloneDeep(resolvedData)
if (fields.length === 0) {
this.wasSuccessful = false
defaults = clonedData
Object.assign(this, resolvedData)
} else {
Expand Down Expand Up @@ -169,7 +170,6 @@ export default function useForm<TForm extends FormDataType>(
recentlySuccessfulTimeoutId = setTimeout(() => (this.recentlySuccessful = false), 2000)

const onSuccess = options.onSuccess ? await options.onSuccess(page) : null
defaults = cloneDeep(this.data())
this.isDirty = false
return onSuccess
},
Expand Down Expand Up @@ -240,7 +240,7 @@ export default function useForm<TForm extends FormDataType>(
watch(
form,
(newValue) => {
form.isDirty = !isEqual(form.data(), defaults)
form.isDirty = !form.wasSuccessful && !isEqual(form.data(), defaults)
if (rememberKey) {
router.remember(newValue.__remember(), rememberKey)
}
Expand Down

0 comments on commit ee1aea7

Please sign in to comment.