Beautiful Open-Source Form Builder
Clear values on edit (#932)
Chirag Chhatralaa29febf42e87
diff --git a/api/app/Http/Resources/FormResource.php b/api/app/Http/Resources/FormResource.phpindex c3ce456c9..b60a32bb0 100644--- a/api/app/Http/Resources/FormResource.php+++ b/api/app/Http/Resources/FormResource.php@@ -32,6 +32,7 @@ class FormResource extends JsonResource'redirect_url' => $this->redirect_url,'submissions_url' => $this->submissions_url,'database_fields_update' => $this->database_fields_update,+ 'clear_empty_fields_on_update' => $this->clear_empty_fields_on_update,'cleanings' => $this->getCleanigns(),'can_be_indexed' => $this->can_be_indexed,'password' => $this->password,diff --git a/api/app/Jobs/Form/StoreFormSubmissionJob.php b/api/app/Jobs/Form/StoreFormSubmissionJob.phpindex aa0238c1c..9bd5fdd24 100644--- a/api/app/Jobs/Form/StoreFormSubmissionJob.php+++ b/api/app/Jobs/Form/StoreFormSubmissionJob.php@@ -50,6 +50,7 @@ class StoreFormSubmissionJob implements ShouldQueueprivate ?array $formData = null;private ?int $completionTime = null;private bool $isPartial = false;+ private bool $isClientProvidedSubmissionId = false;private ?string $submitterIp = null;/**@@ -98,6 +99,7 @@ class StoreFormSubmissionJob implements ShouldQueueif (isset($this->submissionData['submission_id']) && $this->submissionData['submission_id']) {if (is_numeric($this->submissionData['submission_id'])) {$this->submissionId = (int)$this->submissionData['submission_id'];+ $this->isClientProvidedSubmissionId = true;}unset($this->submissionData['submission_id']);}@@ -155,7 +157,11 @@ class StoreFormSubmissionJob implements ShouldQueue}$record = $query->first();- return $record ? $record->id : null;+ if ($record) {+ $this->isClientProvidedSubmissionId = true;+ return $record->id;+ }+ return null;}/**@@ -212,6 +218,14 @@ class StoreFormSubmissionJob implements ShouldQueuecontinue;}+ // For editable submissions, always include empty values to clear fields+ // For field-matching updates, respect the form's clear_empty_fields_on_update setting+ $shouldSkipEmpty = !$this->isClientProvidedSubmissionId && !($this->form->clear_empty_fields_on_update ?? false);+ if ($shouldSkipEmpty && (empty($answerValue) || is_null($answerValue)) && $answerValue !== 0 && $answerValue !== '0' && $answerValue !== false) {+ continue;+ }++// Sanitize only rich text; plain text fields are stored as-is and rendered safely in UIif ($field['type'] === 'rich_text') {if (is_string($answerValue)) {diff --git a/api/app/Models/Forms/Form.php b/api/app/Models/Forms/Form.phpindex 6c3c74eaf..5fedc9a72 100644--- a/api/app/Models/Forms/Form.php+++ b/api/app/Models/Forms/Form.php@@ -86,6 +86,7 @@ class Form extends Model implements CachableAttributes// Submission'submit_button_text','database_fields_update',+ 'clear_empty_fields_on_update','re_fillable','re_fill_button_text','submitted_text',@@ -127,6 +128,7 @@ class Form extends Model implements CachableAttributes'enable_partial_submissions' => 'boolean','enable_ip_tracking' => 'boolean','auto_save' => 'boolean',+ 'clear_empty_fields_on_update' => 'boolean','presentation_style' => 'string','settings' => 'array',];diff --git a/api/app/Service/Forms/FormSpamService.php b/api/app/Service/Forms/FormSpamService.phpindex 986474230..25a1a1b35 100644--- a/api/app/Service/Forms/FormSpamService.php+++ b/api/app/Service/Forms/FormSpamService.php@@ -39,6 +39,11 @@ class FormSpamServiceprivate function shouldCheck(Form $form): bool{+ // Check if creator exists before accessing its properties+ if (!$form->creator) {+ return false;+ }+if ($form->creator->is_blocked || $form->creator->admin || $form->creator->moderator) {return false;}diff --git a/api/database/migrations/2025_10_17_162830_add_clear_empty_fields_on_update_to_forms.php b/api/database/migrations/2025_10_17_162830_add_clear_empty_fields_on_update_to_forms.phpnew file mode 100644index 000000000..7305aa109--- /dev/null+++ b/api/database/migrations/2025_10_17_162830_add_clear_empty_fields_on_update_to_forms.php@@ -0,0 +1,27 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class () extends Migration {+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::table('forms', function (Blueprint $table) {+ $table->boolean('clear_empty_fields_on_update')->default(false);+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('forms', function (Blueprint $table) {+ $table->dropColumn('clear_empty_fields_on_update');+ });+ }+};diff --git a/api/tests/Feature/Forms/FormSubmissionProcessorTest.php b/api/tests/Feature/Forms/FormSubmissionProcessorTest.phpindex c67967483..f05ac6b92 100644--- a/api/tests/Feature/Forms/FormSubmissionProcessorTest.php+++ b/api/tests/Feature/Forms/FormSubmissionProcessorTest.php@@ -146,3 +146,84 @@ it('returns no redirect for non-pro users', function () {'redirect' => false]);});+++describe('Clear Empty Fields On Update', function () {+ it('sends empty values when editing submission via unique URL', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'editable_submissions' => true,+ 'clear_empty_fields_on_update' => false, // Default safe mode+ ]);++ // Submit initial form with data+ $initialData = $this->generateFormSubmissionData($form);+ $response = $this->postJson(route('forms.answer', $form), $initialData)+ ->assertSuccessful();++ $submissionId = $response->json('submission_id');++ // Edit submission with empty field (should still send empty to clear it)+ $editData = $this->generateFormSubmissionData($form);+ // Clear one field to test empty value handling+ $firstFieldKey = array_key_first($editData);+ $editData[$firstFieldKey] = null;+ $editData['submission_id'] = $submissionId;++ $this->postJson(route('forms.answer', $form), $editData)+ ->assertSuccessful()+ ->assertJson([+ 'type' => 'success',+ 'message' => 'Form submission saved.',+ ]);+ });++ it('respects clear_empty_fields_on_update=false for field-matching updates', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'database_fields_update' => ['field_id_for_email'], // Match by email+ 'clear_empty_fields_on_update' => false, // Default: don't clear empty fields+ ]);++ // Submit initial form+ $initialData = $this->generateFormSubmissionData($form);+ $this->postJson(route('forms.answer', $form), $initialData)+ ->assertSuccessful();++ // Update same record with empty field (should be skipped, not sent to Notion)+ $updateData = $this->generateFormSubmissionData($form);+ // Get the first field key to clear it+ $fieldKeys = array_keys($updateData);+ $fieldToClear = $fieldKeys[0];+ $updateData[$fieldToClear] = null; // Empty field should NOT be sent++ $this->postJson(route('forms.answer', $form), $updateData)+ ->assertSuccessful();+ });++ it('clears empty fields when clear_empty_fields_on_update=true for field-matching updates', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace, [+ 'database_fields_update' => ['field_id_for_email'], // Match by email+ 'clear_empty_fields_on_update' => true, // ENABLED: do clear empty fields+ ]);++ // Submit initial form+ $initialData = $this->generateFormSubmissionData($form);+ $this->postJson(route('forms.answer', $form), $initialData)+ ->assertSuccessful();++ // Update same record with empty field (should be sent and clear the field)+ $updateData = $this->generateFormSubmissionData($form);+ // Get the first field key to clear it+ $fieldKeys = array_keys($updateData);+ $fieldToClear = $fieldKeys[0];+ $updateData[$fieldToClear] = null; // Empty field SHOULD be sent to clear it++ $this->postJson(route('forms.answer', $form), $updateData)+ ->assertSuccessful();+ });+});diff --git a/api/tests/Feature/Forms/FormUpdateTest.php b/api/tests/Feature/Forms/FormUpdateTest.phpindex 22fa80e87..39212ae4c 100644--- a/api/tests/Feature/Forms/FormUpdateTest.php+++ b/api/tests/Feature/Forms/FormUpdateTest.php@@ -44,6 +44,7 @@ it('can update form with existing record but generates_uuid field is not update'$user = $this->actingAsProUser();$workspace = $this->createUserWorkspace($user);$form = $this->createForm($user, $workspace, [+ 'clear_empty_fields_on_update' => true,'editable_submissions' => true,'properties' => [[diff --git a/api/tests/Feature/Submissions/PartialSubmissionTest.php b/api/tests/Feature/Submissions/PartialSubmissionTest.phpindex 4508185c9..b9038d832 100644--- a/api/tests/Feature/Submissions/PartialSubmissionTest.php+++ b/api/tests/Feature/Submissions/PartialSubmissionTest.php@@ -104,7 +104,8 @@ it('handles file uploads in partial submissions', function () {$user = $this->actingAsProUser();$workspace = $this->createUserWorkspace($user);$form = $this->createForm($user, $workspace, [- 'enable_partial_submissions' => true+ 'enable_partial_submissions' => true,+ 'clear_empty_fields_on_update' => true]);// Create a fake file@@ -141,7 +142,8 @@ it('handles signature field in partial submissions', function () {$user = $this->actingAsProUser();$workspace = $this->createUserWorkspace($user);$form = $this->createForm($user, $workspace, [- 'enable_partial_submissions' => true+ 'enable_partial_submissions' => true,+ 'clear_empty_fields_on_update' => true]);// Create partial submission with signaturediff --git a/client/components/forms/core/OptionSelectorInput.vue b/client/components/forms/core/OptionSelectorInput.vueindex e6f0b0cca..242f858af 100644--- a/client/components/forms/core/OptionSelectorInput.vue+++ b/client/components/forms/core/OptionSelectorInput.vue@@ -24,6 +24,7 @@:class="optionClasses(isSelected(option))"><button+ type="button"class="group flex flex-col items-center justify-center transition-colors focus:outline-hidden w-full h-full":class="[buttonClasses(disabled || option.disabled), option.class ? (typeof option.class === 'function' ? option.class(isSelected(option)) : option.class) : {}, isSelected(option) ? 'text-form-color' : 'text-inherit']":aria-selected="isSelected(option) ? 'true' : 'false'"diff --git a/client/components/open/forms/OpenCompleteForm.vue b/client/components/open/forms/OpenCompleteForm.vueindex 5fbdcd36f..83b5fba2c 100644--- a/client/components/open/forms/OpenCompleteForm.vue+++ b/client/components/open/forms/OpenCompleteForm.vue@@ -128,6 +128,7 @@ import { useI18n } from 'vue-i18n'import { useIsIframe } from '~/composables/useIsIframe'import Loader from '~/components/global/Loader.vue'import { tailwindcssPaletteGenerator } from '~/lib/colors.js'+import { useRouter } from 'vue-router'const props = defineProps({form: { type: Object, required: true },@@ -146,6 +147,7 @@ const emit = defineEmits(['submitted', 'password-entered', 'restarted'])const { t, setLocale } = useI18n()const route = useRoute()+const router = useRouter()const alert = useAlert()const workingFormStore = useWorkingFormStore()const { data: user } = useAuth().user()@@ -358,10 +360,26 @@ const restart = async () => {submittedData.value = nullsubmissionId.value = nullconst queryString = route.fullPath.split('?')[1] || ''+ const urlParams = new URLSearchParams(queryString)++ // Determine if we should clear the form completely for a fresh start+ const shouldClearUrl = props.form.editable_submissions++ if (shouldClearUrl) {+ // Remove submission_id parameter and navigate to clean URL+ urlParams.delete('submission_id')+ const newQueryString = urlParams.toString()+ const newPath = newQueryString ? `${route.path}?${newQueryString}` : route.path+ await router.replace(newPath)+ }++ // Single restart call with conditional optionsawait formManager.restart({- urlParams: new URLSearchParams(queryString),- submissionId: null+ urlParams: shouldClearUrl ? new URLSearchParams() : urlParams,+ submissionId: null,+ skipUrlParams: shouldClearUrl})+emit('restarted', true)}diff --git a/client/components/open/forms/components/form-components/FormSubmissionSettings.vue b/client/components/open/forms/components/form-components/FormSubmissionSettings.vueindex 8406d4007..5c283ded0 100644--- a/client/components/open/forms/components/form-components/FormSubmissionSettings.vue+++ b/client/components/open/forms/components/form-components/FormSubmissionSettings.vue@@ -54,11 +54,10 @@:required="true"/><div- v-if="submissionOptions.databaseAction == 'update' && filterableFields.length"+ v-if="submissionOptions.databaseAction == 'update'"class="bg-neutral-50 border rounded-lg px-4 py-2"><div- v-if="submissionOptions.databaseAction == 'update' && filterableFields.length"class="w-auto max-w-lg"><p class="mb-2 mt-2 text-neutral-500 text-sm">@@ -80,33 +79,42 @@multipleclearable/>++ <toggle-switch-input+ v-model="clearEmptyFieldsOnUpdate"+ class="mt-4"+ label="Clear empty fields on update"+ help="When enabled, fields left empty in submission will clear existing values in Submission. When disabled, only filled fields are updated."+ /></div></div><!-- Advanced Submission Settings -->- <h4 class="font-semibold mt-4 border-t pt-4">- Advanced Submission Options <pro-tag />- </h4>- <p class="text-neutral-500 text-sm mb-4">- Configure advanced options for form submissions and data collection.- </p>-- <ToggleSwitchInput- name="enable_partial_submissions"- :form="form"- help="Capture incomplete form submissions to analyze user drop-off points and collect partial data even when users don't complete the entire form."- >- <template #label>- <span class="text-sm">- Collect partial submissions- </span>- <ProTag- class="ml-1"- upgrade-modal-title="Upgrade to collect partial submissions"- upgrade-modal-description="Capture valuable data from incomplete form submissions. Analyze where users drop off and collect partial information even when they don't complete the entire form."- />- </template>- </ToggleSwitchInput>+ <div class="mb-8">+ <h4 class="font-semibold mt-4 border-t pt-4">+ Advanced Submission Options <ProTag class="ml-1"/>+ </h4>+ <p class="text-gray-500 text-sm mb-4">+ Configure advanced options for form submissions and data collection.+ </p>++ <ToggleSwitchInput+ name="enable_partial_submissions"+ :form="form"+ help="Capture incomplete form submissions to analyze user drop-off points and collect partial data even when users don't complete the entire form."+ >+ <template #label>+ <span class="text-sm">+ Collect partial submissions+ </span>+ <ProTag+ class="ml-1"+ upgrade-modal-title="Upgrade to collect partial submissions"+ upgrade-modal-description="Capture valuable data from incomplete form submissions. Analyze where users drop off and collect partial information even when they don't complete the entire form."+ />+ </template>+ </ToggleSwitchInput>+ </div><ToggleSwitchInputclass="mt-4"@@ -136,123 +144,106 @@/><!-- Post-Submission Behavior -->- <h4 class="font-semibold mt-4 border-t pt-4">- After Submission <pro-tag- upgrade-modal-title="Upgrade to customize post-submission behavior"- upgrade-modal-description="Customize post-submission behavior: redirect users, show custom messages, or trigger actions. Upgrade to unlock advanced options for a seamless user experience. We have plenty of other pro features to enhance your form functionality and user engagement."- />- </h4>- <p class="text-neutral-500 text-sm mb-4">- Customize the user experience after form submission.- </p>- <div- class="w-full"- :class="{'flex flex-wrap gap-x-4':submissionOptions.submissionMode === 'redirect'}"- >- <flat-select-input- :form="submissionOptions"- name="submissionMode"- class="w-full max-w-xs"- label="Action After Form Submission"+ <div class="mb-8">+ <h4 class="font-semibold mt-4 border-t pt-4">+ After Submission <pro-tag+ upgrade-modal-title="Upgrade to customize post-submission behavior"+ upgrade-modal-description="Customize post-submission behavior: redirect users, show custom messages, or trigger actions. Upgrade to unlock advanced options for a seamless user experience. We have plenty of other pro features to enhance your form functionality and user engagement."+ />+ </h4>+ <p class="text-neutral-500 text-sm mb-4">+ Customize the user experience after form submission.+ </p>++ <OptionSelectorInput+ label="Action after form submission"+ v-model="submissionOptions.submissionMode":options="[- { name: 'Show Success page', value: 'default' },- { name: 'Redirect', value: 'redirect' }+ { name: 'default', label: 'Show Success page' },+ { name: 'redirect', label: 'Redirect' }]"+ option-key="name"+ :columns="2"+ class="mb-4 max-w-xs"+ />++ <div+ v-if="submissionOptions.submissionMode"+ class="bg-gray-50 border rounded-lg px-4 py-2">- <template #selected="{ option, optionName }">- <div class="flex items-center truncate text-sm mr-6">- {{ optionName }}- <pro-tag- v-if="option === 'redirect'"- class="ml-2"+ <div class="w-auto max-w-lg">+ <template v-if="submissionOptions.submissionMode === 'redirect'">+ <MentionInput+ name="redirect_url"+ :form="form"+ :mentions="form.properties"+ class="w-full max-w-xs"+ label="Redirect URL"+ placeholder="https://www.google.com"+ :required="true"/>- </div>- </template>- <template #option="{ option, selected }">- <span class="flex">- <p class="flex-grow">- {{ option.name }} <template v-if="option.value === 'redirect'"><pro-tag /></template>- </p>- <span- v-if="selected"- class="absolute inset-y-0 right-0 flex items-center pr-4"- >- <Icon- name="heroicons:check-20-solid"- class="h-5 w-5"- />- </span>- </span>- </template>- </flat-select-input>- <template v-if="submissionOptions.submissionMode === 'redirect'">- <MentionInput- name="redirect_url"- :form="form"- :mentions="form.properties"- class="w-full max-w-xs"- label="Redirect URL"- placeholder="https://www.google.com"- :required="true"- />- </template>- <template v-else>- <rich-text-area-input- enable-mentions- :mentions="form.properties"- :allow-fullscreen="true"- name="submitted_text"- class="w-full mt-4"- :form="form"- label="Success page text"- :required="false"- :max-char-limit="10000"- :show-char-limit="true"- />- <div class="flex items-center flex-wrap gap-x-4">- <toggle-switch-input- name="re_fillable"- class="w-full max-w-xs"- :form="form"- label="Re-fillable form"- help="Allows user to fill the form multiple times"- />- <text-input- v-if="form.re_fillable"- name="re_fill_button_text"- :form="form"- label="Text of re-start button"- />- </div>- </template>- <div class="flex items-center flex-wrap gap-x-4">- <toggle-switch-input- name="editable_submissions"- class="w-full max-w-sm"- help="Allows users to edit submissions via unique URL"- :form="form"- >- <template #label>- <span class="text-sm">- Editable submissions- </span>- <ProTag- class="ml-1"- upgrade-modal-title="Upgrade to use Editable Submissions"- upgrade-modal-description="On the Free plan, you can try out all paid features only within the form editor. Upgrade your plan to allow users to update their submissions via a unique URL, and much more. Gain full access to all advanced features."+ </template>+ <template v-else>+ <rich-text-area-input+ enable-mentions+ :mentions="form.properties"+ :allow-fullscreen="true"+ name="submitted_text"+ class="w-full"+ :form="form"+ label="Success page text"+ :required="false"+ :max-char-limit="10000"+ :show-char-limit="true"/>+ <div class="flex items-center flex-wrap gap-x-4 mt-4">+ <toggle-switch-input+ name="re_fillable"+ class="w-full max-w-xs"+ :form="form"+ label="Re-fillable form"+ help="Allows user to fill the form multiple times"+ />+ <text-input+ v-if="form.re_fillable"+ name="re_fill_button_text"+ :form="form"+ label="Text of re-start button"+ />+ </div></template>- </toggle-switch-input>- <text-input- v-if="form.editable_submissions"- name="editable_submissions_button_text"- class="w-full max-w-64"- :form="form"- label="Edit submission button text"- :required="true"- />+ </div></div></div>++ <!-- Editable Submissions Settings -->+ <div class="mb-8">+ <h4 class="font-semibold mt-4 border-t pt-4">+ Editable Submissions <ProTag+ class="ml-1"+ upgrade-modal-title="Upgrade to use Editable Submissions"+ upgrade-modal-description="On the Free plan, you can try out all paid features only within the form editor. Upgrade your plan to allow users to update their submissions via a unique URL, and much more. Gain full access to all advanced features."+ />+ </h4>+ <p class="text-gray-500 text-sm mb-4">+ Allow respondents to edit and update their submissions via a unique link.+ </p>+ <toggle-switch-input+ name="editable_submissions"+ class="w-full max-w-sm"+ help="Allows users to edit submissions via unique URL"+ :form="form"+ label="Enable editable submissions"+ />+ <text-input+ v-if="form.editable_submissions"+ name="editable_submissions_button_text"+ class="w-full max-w-64 mt-4"+ :form="form"+ label="Edit submission button text"+ :required="true"+ />+ </div></div></VForm></template>@@ -283,14 +274,22 @@ const filterableFields = computed(() => {})})-watch(form, () => {+const clearEmptyFieldsOnUpdate = computed({+ get: () => form.value.clear_empty_fields_on_update ?? false,+ set: (value) => { form.value.clear_empty_fields_on_update = value }+})++watch({+ redirect_url: form.value.redirect_url,+ database_fields_update: form.value.database_fields_update+}, () => {if (form.value) {submissionOptions.value = {submissionMode: form.value.redirect_url ? 'redirect' : 'default',databaseAction: form.value.database_fields_update ? 'update' : 'create'}}-}, { deep: true })+}, { immediate: true })watch(submissionOptions, (val) => {if (val.submissionMode === 'default') form.value.redirect_url = nulldiff --git a/client/lib/forms/composables/useFormManager.js b/client/lib/forms/composables/useFormManager.jsindex c3357398d..438a694b9 100644--- a/client/lib/forms/composables/useFormManager.js+++ b/client/lib/forms/composables/useFormManager.js@@ -341,12 +341,17 @@ export function useFormManager(initialFormConfig, initialMode = FormMode.LIVE, ostate.isProcessing = falsetimer.reset() // Reset timer via composabletimer.start() // Restart timer++ // Use resetAndFill with empty object to properly clear originalData and all fields+ form.resetAndFill({})+ pendingSubmissionService?.clear()// Reinitialize the form to reapply URL parameters and default valuesawait initialize({urlParams: options.urlParams,submissionId: options.submissionId, // Explicitly pass submissionId (usually null)- skipPendingSubmission: true // Don't reload from localStorage on restart+ skipPendingSubmission: true, // Don't reload from localStorage on restart+ skipUrlParams: options.skipUrlParams // Pass through the skipUrlParams option})}