Skip to content

Beautiful Open-Source Form Builder

Refactor form submission status handling to prevent reverting completed submissions

JhumanJa06dcc9b2837
diff --git a/api/app/Jobs/Form/StoreFormSubmissionJob.php b/api/app/Jobs/Form/StoreFormSubmissionJob.php
index 9bd5fdd24..4eca016f6 100644
--- a/api/app/Jobs/Form/StoreFormSubmissionJob.php
+++ b/api/app/Jobs/Form/StoreFormSubmissionJob.php
@@ -184,9 +184,18 @@ class StoreFormSubmissionJob implements ShouldQueue
}
$submission->data = $formData;
$submission->completion_time = $this->completionTime;
- $submission->status = $this->isPartial
- ? FormSubmission::STATUS_PARTIAL
- : FormSubmission::STATUS_COMPLETED;
+
+ // Determine submission status
+ // Never allow a completed submission to be reverted to partial (prevents race conditions)
+ if ($this->isPartial) {
+ // Only set partial status if submission is new OR not already completed
+ if (!$this->submissionId || $submission->status !== FormSubmission::STATUS_COMPLETED) {
+ $submission->status = FormSubmission::STATUS_PARTIAL;
+ }
+ // If already completed, keep it completed (ignore the partial flag)
+ } else {
+ $submission->status = FormSubmission::STATUS_COMPLETED;
+ }
// Store IP address in meta if IP tracking is enabled
if ($this->form->enable_ip_tracking && $this->form->is_pro && $this->submitterIp) {
diff --git a/api/tests/Feature/Submissions/PartialSubmissionTest.php b/api/tests/Feature/Submissions/PartialSubmissionTest.php
index b9038d832..145168d5a 100644
--- a/api/tests/Feature/Submissions/PartialSubmissionTest.php
+++ b/api/tests/Feature/Submissions/PartialSubmissionTest.php
@@ -235,3 +235,44 @@ it('submits as completed on non-pro forms', function () {
$submission = FormSubmission::first();
expect($submission->status)->toBe(FormSubmission::STATUS_COMPLETED);
});
+
+it('cannot revert a completed submission back to partial', function () {
+ $user = $this->actingAsProUser();
+ $workspace = $this->createUserWorkspace($user);
+ $form = $this->createForm($user, $workspace, [
+ 'enable_partial_submissions' => true
+ ]);
+
+ // Step 1: Create a partial submission
+ $formData = $this->generateFormSubmissionData($form, ['text' => 'Initial']);
+ $formData['is_partial'] = true;
+
+ $partialResponse = $this->postJson(route('forms.answer', $form->slug), $formData)
+ ->assertSuccessful();
+
+ $submissionHash = $partialResponse->json('submission_hash');
+
+ // Step 2: Complete the submission
+ $completeData = $this->generateFormSubmissionData($form, ['text' => 'Complete']);
+ $completeData['submission_hash'] = $submissionHash;
+
+ $this->postJson(route('forms.answer', $form->slug), $completeData)
+ ->assertSuccessful();
+
+ // Verify it's completed
+ $submission = FormSubmission::first();
+ expect($submission->status)->toBe(FormSubmission::STATUS_COMPLETED);
+
+ // Step 3: Try to send another partial submission with the same hash
+ // This simulates a race condition where a delayed partial request arrives after completion
+ $latePartialData = $this->generateFormSubmissionData($form, ['text' => 'Late partial update']);
+ $latePartialData['is_partial'] = true;
+ $latePartialData['submission_hash'] = $submissionHash;
+
+ $this->postJson(route('forms.answer', $form->slug), $latePartialData)
+ ->assertSuccessful();
+
+ // Verify the submission status was NOT reverted to partial
+ $submission->refresh();
+ expect($submission->status)->toBe(FormSubmission::STATUS_COMPLETED);
+});
diff --git a/client/composables/forms/usePartialSubmission.js b/client/composables/forms/usePartialSubmission.js
index faa1fa753..ad966fdb6 100644
--- a/client/composables/forms/usePartialSubmission.js
+++ b/client/composables/forms/usePartialSubmission.js
@@ -35,6 +35,18 @@ export function usePartialSubmission(formConfig, formDataRef, pendingSubmissionS
}
}
+ /**
+ * Clears the submission hash from both localStorage and memory.
+ * Call this after successful form submission to prevent stale data.
+ */
+ const clearSubmissionHash = () => {
+ pendingSubmissionService.setSubmissionHash(null)
+ const key = pendingSubmissionService.formPendingSubmissionKey?.value
+ if (key) {
+ submissionHashes.value.delete(key)
+ }
+ }
+
const debouncedSync = () => {
// Clear existing timeout to reset the timer
if (syncTimeout) {
@@ -126,11 +138,20 @@ export function usePartialSubmission(formConfig, formDataRef, pendingSubmissionS
window.addEventListener('beforeunload', handleBeforeUnload)
}
- const stopSync = () => {
+ /**
+ * Stops the partial submission sync.
+ * @param {Object} options - Options for stopping
+ * @param {boolean} options.skipFinalSync - If true, skip the final sync (use when about to do a complete submission)
+ */
+ const stopSync = (options = {}) => {
if (import.meta.server) return
- // Final sync before stopping if we have a hash
- if (getSubmissionHash()) {
+ const { skipFinalSync = false } = options
+
+ // Only sync before stopping if we have a hash AND we're not skipping
+ // Skip when doing a final submission to avoid race conditions where
+ // the partial submission (is_partial: true) arrives after the final submission
+ if (!skipFinalSync && getSubmissionHash()) {
syncImmediately()
}
@@ -166,6 +187,7 @@ export function usePartialSubmission(formConfig, formDataRef, pendingSubmissionS
syncToServer: debouncedSync, // Expose the debounced version externally
syncImmediately, // Also expose the immediate sync for critical situations
getSubmissionHash, // Use the getter that prioritizes localStorage
- setSubmissionHash // Use the setter that updates both
+ setSubmissionHash, // Use the setter that updates both
+ clearSubmissionHash // Clear hash from both localStorage and memory after successful submission
}
}
\ No newline at end of file
diff --git a/client/lib/forms/composables/useFormManager.js b/client/lib/forms/composables/useFormManager.js
index c5a3c41a9..bea55fd5e 100644
--- a/client/lib/forms/composables/useFormManager.js
+++ b/client/lib/forms/composables/useFormManager.js
@@ -233,8 +233,11 @@ export function useFormManager(initialFormConfig, initialMode = FormMode.LIVE, o
try {
// Stop partial submission sync during submission if enabled
+ // Use skipFinalSync: true to prevent a race condition where the partial submission
+ // (with is_partial: true) arrives at the server after the final submission,
+ // overwriting the 'completed' status back to 'partial'
if (!import.meta.server && toValue(config).enable_partial_submissions && strategy.value.submission.enablePartialSubmissions) {
- partialSubmissionService.stopSync() // This will sync immediately before stopping
+ partialSubmissionService.stopSync({ skipFinalSync: true })
}
// 1. Stop Timer & Get Time
@@ -286,7 +289,10 @@ export function useFormManager(initialFormConfig, initialMode = FormMode.LIVE, o
// 7. Clear pending submission data on successful submit
pendingSubmissionService?.clear()
- // 8. Handle amplitude logging
+ // 8. Clear partial submission hash to prevent stale data
+ partialSubmissionService?.clearSubmissionHash()
+
+ // 9. Handle amplitude logging
if (import.meta.client) {
const amplitude = useAmplitude()
amplitude.logEvent('form_submission', {
@@ -295,7 +301,7 @@ export function useFormManager(initialFormConfig, initialMode = FormMode.LIVE, o
})
}
- // 9. Handle postMessage communication for iframe integration
+ // 10. Handle postMessage communication for iframe integration
if (import.meta.client) {
const isIframe = useIsIframe()
const formConfig = toValue(config)
@@ -320,7 +326,7 @@ export function useFormManager(initialFormConfig, initialMode = FormMode.LIVE, o
window.postMessage(payload, '*')
}
- // 10. Handle redirect if server response includes redirect info
+ // 11. Handle redirect if server response includes redirect info
if (import.meta.client && submissionResult?.redirect && submissionResult?.redirect_url) {
window.location.href = submissionResult.redirect_url
}