Skip to content

Beautiful Open-Source Form Builder

Refactor form initialization and data management in OpenForm component (#709)

Julien Nahum267beec58277
diff --git a/client/components/open/forms/OpenForm.vue b/client/components/open/forms/OpenForm.vue
index 59659ab95..dc9233a30 100644
--- a/client/components/open/forms/OpenForm.vue
+++ b/client/components/open/forms/OpenForm.vue
@@ -131,7 +131,6 @@ import CaptchaInput from '~/components/forms/components/CaptchaInput.vue'
import OpenFormField from './OpenFormField.vue'
import {pendingSubmission} from "~/composables/forms/pendingSubmission.js"
import FormLogicPropertyResolver from "~/lib/forms/FormLogicPropertyResolver.js"
-import {computed} from "vue"
import CachedDefaultTheme from "~/lib/forms/themes/CachedDefaultTheme.js"
import FormTimer from './FormTimer.vue'
import { storeToRefs } from 'pinia'
@@ -439,14 +438,19 @@ export default {
*/
async initForm() {
if (this.defaultDataForm) {
- this.dataForm = useForm(this.defaultDataForm)
+ await nextTick(() => {
+ this.dataForm.resetAndFill(this.defaultDataForm)
+ })
return
}
-
+
if (await this.tryInitFormFromEditableSubmission()) return
if (this.tryInitFormFromPendingSubmission()) return
- this.initFormWithDefaultValues()
+ await nextTick(() => {
+ this.formPageIndex = 0
+ this.initFormWithDefaultValues()
+ })
},
async tryInitFormFromEditableSubmission() {
if (this.isPublicFormPage && this.form.editable_submissions) {
@@ -455,7 +459,7 @@ export default {
this.form.submission_id = submissionId
const data = await this.getSubmissionData()
if (data) {
- this.dataForm = useForm(data)
+ this.dataForm.resetAndFill(data)
return true
}
}
@@ -467,7 +471,7 @@ export default {
const pendingData = this.pendingSubmission.get()
if (pendingData && Object.keys(pendingData).length !== 0) {
this.updatePendingDataFields(pendingData)
- this.dataForm = useForm(pendingData)
+ this.dataForm.resetAndFill(pendingData)
return true
}
}
@@ -481,7 +485,7 @@ export default {
})
},
initFormWithDefaultValues() {
- const formData = clonedeep(this.dataForm?.data() || {})
+ const formData = {}
const urlPrefill = this.isPublicFormPage ? new URLSearchParams(window.location.search) : null
this.fields.forEach(field => {
@@ -491,7 +495,7 @@ export default {
this.handleDefaultPrefill(field, formData)
})
- this.dataForm = useForm(formData)
+ this.dataForm.resetAndFill(formData)
},
handleUrlPrefill(field, formData, urlPrefill) {
if (!urlPrefill) return
diff --git a/client/composables/lib/vForm/Form.js b/client/composables/lib/vForm/Form.js
index cab2744cc..bcfa6ba8c 100644
--- a/client/composables/lib/vForm/Form.js
+++ b/client/composables/lib/vForm/Form.js
@@ -85,6 +85,17 @@ class Form {
clearTimeout(this.recentlySuccessfulTimeoutId)
}
+ resetAndFill(data = {}) {
+ // Clear form state
+ this.clear()
+
+ // Reset and update form data using the existing update method
+ this.originalData = {}
+ this.update(data)
+
+ return this
+ }
+
reset() {
Object.keys(this)
.filter((key) => !Form.ignore.includes(key))