Skip to content

Beautiful Open-Source Form Builder

Enhance form input handling and UI components

JhumanJ8b471b94a0b4
diff --git a/client/components/forms/core/ToggleSwitchInput.vue b/client/components/forms/core/ToggleSwitchInput.vue
index 582df99f8..d98dcd3d1 100644
--- a/client/components/forms/core/ToggleSwitchInput.vue
+++ b/client/components/forms/core/ToggleSwitchInput.vue
@@ -35,6 +35,7 @@
:label="label"
:required="required"
:theme="theme"
+ :native-for="id ? id : name"
/>
</slot>
<slot
diff --git a/client/components/forms/useFormInput.js b/client/components/forms/useFormInput.js
index e1478c7aa..57da52a8f 100644
--- a/client/components/forms/useFormInput.js
+++ b/client/components/forms/useFormInput.js
@@ -85,7 +85,30 @@ export function useFormInput(props, context, options = {}) {
},
set: (val) => {
if (props.form) {
- _set(props.form, (composableOptions.formPrefixKey || "") + props.name, val)
+ const fullPath = (composableOptions.formPrefixKey || "") + props.name
+ const pathParts = fullPath.split('.')
+
+ // Ensure all parent objects exist before setting nested properties.
+ // This is necessary for deeply nested form field paths (e.g., 'address.street.number')
+ // where intermediate objects may not exist yet. Without this, lodash _set() would fail
+ // or create the nested structure incorrectly on some form backends.
+ //
+ // Edge case handling:
+ // - If a path segment exists but is not an object (null, primitive value), it gets
+ // replaced with an empty object so the nested structure can be created.
+ // - This ensures the form data structure is always valid for deeply nested fields.
+ if (pathParts.length > 1) {
+ let current = props.form
+ for (let i = 0; i < pathParts.length - 1; i++) {
+ const part = pathParts[i]
+ if (!current[part] || typeof current[part] !== 'object') {
+ current[part] = {}
+ }
+ current = current[part]
+ }
+ }
+
+ _set(props.form, fullPath, val)
} else {
content.value = val
}
diff --git a/client/components/open/forms/OpenFormFocused.vue b/client/components/open/forms/OpenFormFocused.vue
index 8bc91bd75..bee721dcb 100644
--- a/client/components/open/forms/OpenFormFocused.vue
+++ b/client/components/open/forms/OpenFormFocused.vue
@@ -49,7 +49,7 @@
<div class="relative">
<BlockRenderer :block="currentBlock" :form-manager="formManager" />
</div>
- <div v-if="!props.formManager?.state.isSubmitted" class="mt-2 flex gap-2 justify-start" :class="{'flex-col justify-normal! items-center': isLast &&form.use_captcha}">
+ <div v-if="!props.formManager?.state.isSubmitted" class="mt-2 flex gap-2" :class="[getFieldAlignClasses(currentBlock), {'flex-col justify-normal! items-center': isLast &&form.use_captcha}]">
<slot name="submit-btn" v-if="isLast" :loading="isProcessing">
<CaptchaWrapper v-if="form.use_captcha" :form-manager="formManager" />
<open-form-button :form="form" class="mt-0.5 px-6" :loading="isProcessing" @click.prevent="emit('submit')">
@@ -219,4 +219,12 @@ const goPrev = () => {
}
}
const goNext = () => { if (!isLast.value) handleNextClick() }
+
+// If block is text block, or any other block that has an align property, use the align property to determine the justify class
+function getFieldAlignClasses(field) {
+ if (!field?.align || field.align === 'left') return 'justify-start'
+ else if (field.align === 'right') return 'justify-end'
+ else if (field.align === 'center') return 'justify-center'
+ else return 'justify-start'
+}
</script>
diff --git a/client/components/open/forms/components/form-components/FormCustomization.vue b/client/components/open/forms/components/form-components/FormCustomization.vue
index 5929969ac..a352a2c4e 100644
--- a/client/components/open/forms/components/form-components/FormCustomization.vue
+++ b/client/components/open/forms/components/form-components/FormCustomization.vue
@@ -192,7 +192,7 @@
<toggle-switch-input
v-if="isFocused"
- v-model="navigationArrows"
+ name="settings.navigation_arrows"
:form="form"
class="mt-2"
label="Show navigation arrows"
@@ -260,19 +260,6 @@ const availableLocales = computed(() => {
return $i18n.locales?.value.map(locale => ({ name: locale.name, value: locale.code })) ?? []
})
-// Bind navigation arrows robustly even if settings is missing
-const navigationArrows = computed({
- get() {
- return form.value?.settings?.navigation_arrows ?? true
- },
- set(val) {
- if (!form.value) return
- const currentSettings = form.value.settings ?? {}
- // Reassign the whole settings object to ensure reactivity
- form.value.settings = { ...currentSettings, navigation_arrows: val }
- }
-})
-
onMounted(() => {
isMounted.value = true
})
diff --git a/client/components/open/forms/components/form-components/FormEditorPreview.vue b/client/components/open/forms/components/form-components/FormEditorPreview.vue
index 221406f8c..570aa7096 100644
--- a/client/components/open/forms/components/form-components/FormEditorPreview.vue
+++ b/client/components/open/forms/components/form-components/FormEditorPreview.vue
@@ -225,6 +225,12 @@ function isValidIndex(index) {
watch(() => currentSlideIndex.value, (newIndex) => {
try {
if (isFocusedEditing.value && isValidIndex(newIndex) && workingFormStore.selectedFieldIndex !== newIndex) {
+ // Skip if the field at this index is hidden
+ const field = workingFormStore.content?.properties?.[newIndex]
+ const struct = workingFormStore.structureService
+ if (field && struct && typeof struct.isFieldHidden === 'function' && struct.isFieldHidden(field)) {
+ return
+ }
// Update the selected field to follow the currently focused slide
workingFormStore.setEditingField(newIndex)
}
@@ -237,7 +243,12 @@ watch(() => currentSlideIndex.value, (newIndex) => {
watch(() => workingFormStore.selectedFieldIndex, (newIndex) => {
try {
if (isFocusedEditing.value && isValidIndex(newIndex)) {
+ // Skip if the selected field is hidden
+ const field = workingFormStore.content?.properties?.[newIndex]
const struct = workingFormStore.structureService
+ if (field && struct && typeof struct.isFieldHidden === 'function' && struct.isFieldHidden(field)) {
+ return
+ }
if (struct && typeof struct.setPageForField === 'function') {
struct.setPageForField(newIndex)
}
diff --git a/client/components/pages/forms/create/CreateFormBaseModal.vue b/client/components/pages/forms/create/CreateFormBaseModal.vue
index e208ee2b4..ae6bdf913 100644
--- a/client/components/pages/forms/create/CreateFormBaseModal.vue
+++ b/client/components/pages/forms/create/CreateFormBaseModal.vue
@@ -25,7 +25,7 @@
>
<div class="text-center mb-4">
<h2 class="text-xl font-bold text-slate-800">Choose a form style</h2>
- <p class="text-slate-500 text-sm">Choose how your form appears to respondents.</p>
+ <p class="text-slate-500 text-sm">Choose how your form appears to respondents.<br/>You can change this later.</p>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div
@@ -253,13 +253,14 @@ function selectStyle(style) {
if (style === 'focused') {
workingFormStore.content.size = 'lg'
// Ensure navigation arrows are enabled by default in focused mode
- const currentSettings = workingFormStore.content.settings ?? {}
- workingFormStore.content.settings = { ...currentSettings, navigation_arrows: true }
+ // Ensure settings object is initialized as a writable object
+ if (!workingFormStore.content.settings || typeof workingFormStore.content.settings !== 'object') {
+ workingFormStore.content.settings = {}
+ }
+ workingFormStore.content.settings.navigation_arrows = true
// Seed first block image to highlight focused mode
seedFocusedFirstBlockImage(workingFormStore.content)
}
- // Close any open sidebars since layout may change
- workingFormStore.closeAllSidebars()
}
currentStep.value = 2
}
diff --git a/client/lib/forms/composables/useFocusedStructure.js b/client/lib/forms/composables/useFocusedStructure.js
index 4a3d5b46b..79c6f0665 100644
--- a/client/lib/forms/composables/useFocusedStructure.js
+++ b/client/lib/forms/composables/useFocusedStructure.js
@@ -3,14 +3,18 @@ import { computed, toValue } from 'vue'
export function useFocusedStructure(formConfig, managerState, formData, fieldState) {
const form = computed(() => toValue(formConfig) || { properties: [] })
- const isHidden = (field) => {
- try { return !!fieldState.getState(field).hidden } catch { return !!field?.hidden }
+ const isFieldHidden = (field) => {
+ try {
+ return !!fieldState.getState(field).hidden
+ } catch {
+ return !!field?.hidden
+ }
}
const visibleProperties = computed(() => {
const properties = form.value.properties || []
// Focused: one field per page; exclude hidden fields so slides are never empty
- return properties.filter(field => !isHidden(field))
+ return properties.filter(field => !isFieldHidden(field))
})
const fieldGroups = computed(() => {
@@ -57,10 +61,27 @@ export function useFocusedStructure(formConfig, managerState, formData, fieldSta
// Utilities used by editor/drag in classic; provide no-ops or simple mapping
const getPageForField = (fieldIndex) => {
- const properties = visibleProperties.value || []
- if (properties.length === 0) return 0
- if (fieldIndex < 0 || fieldIndex >= properties.length) return 0
- return fieldIndex
+ const allProperties = form.value.properties || []
+ const visible = visibleProperties.value || []
+
+ // Get the field from the full properties array
+ const field = allProperties[fieldIndex]
+
+ // If field is hidden, return current page (don't change it)
+ if (field && isFieldHidden(field)) {
+ return managerState?.currentPage ?? 0
+ }
+
+ // Find the index of this field in visible properties
+ const visibleIndex = visible.findIndex(f => f?.id === field?.id)
+
+ if (visibleIndex >= 0) {
+ return visibleIndex
+ }
+
+ // Fallback
+ if (visible.length === 0) return 0
+ return Math.min(fieldIndex, visible.length - 1)
}
const setPageForField = (fieldIndex) => {
@@ -103,7 +124,8 @@ export function useFocusedStructure(formConfig, managerState, formData, fieldSta
getPaymentBlock,
getTargetDropIndex,
determineInsertIndex,
- setPageForField
+ setPageForField,
+ isFieldHidden
}
}
diff --git a/client/stores/working_form.js b/client/stores/working_form.js
index 988e72143..b918d282d 100644
--- a/client/stores/working_form.js
+++ b/client/stores/working_form.js
@@ -120,6 +120,11 @@ export const useWorkingFormStore = defineStore("working_form", {
if (this.selectedFieldIndex !== -1 && previousIndex !== this.selectedFieldIndex) {
// Find which page contains the selected field and set to that page
if (this.structureService) {
+ // Skip if the selected field is hidden
+ const field = this.content?.properties?.[this.selectedFieldIndex]
+ if (field && typeof this.structureService.isFieldHidden === 'function' && this.structureService.isFieldHidden(field)) {
+ return
+ }
this.structureService.setPageForField(this.selectedFieldIndex)
}
}
diff --git a/client/test/unit/useFormInput.test.ts b/client/test/unit/useFormInput.test.ts
new file mode 100644
index 000000000..401707f7c
--- /dev/null
+++ b/client/test/unit/useFormInput.test.ts
@@ -0,0 +1,455 @@
+import { describe, it, expect, beforeEach, vi } from 'vitest'
+import { useFormInput } from '../../components/forms/useFormInput.js'
+
+/**
+ * Test suite for useFormInput composable
+ * Validates form data binding, nested property handling, validation, and model value emission
+ */
+
+describe('useFormInput', () => {
+ let mockForm
+ let mockContext
+
+ beforeEach(() => {
+ // Create a mock form object with validation support
+ mockForm = {
+ simple_field: 'initial_value',
+ nested: {
+ field: 'nested_value'
+ },
+ errors: {
+ has: vi.fn((name) => false),
+ clear: vi.fn()
+ }
+ }
+
+ // Create mock context for emitting events
+ mockContext = {
+ emit: vi.fn()
+ }
+ })
+
+ describe('Simple Property Access', () => {
+ it('should read simple form properties', () => {
+ const props = {
+ name: 'simple_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBe('initial_value')
+ })
+
+ it('should write simple form properties', () => {
+ const props = {
+ name: 'simple_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'new_value'
+ expect(mockForm.simple_field).toBe('new_value')
+ })
+
+ it('should emit update:modelValue when setting simple property', () => {
+ const props = {
+ name: 'simple_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'new_value'
+ expect(mockContext.emit).toHaveBeenCalledWith('update:modelValue', 'new_value')
+ })
+
+ it('should use modelValue when form is not provided', () => {
+ const props = {
+ name: 'field',
+ form: undefined,
+ modelValue: 'local_value'
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBe('local_value')
+ })
+
+ it('should update local content when form is not provided', () => {
+ const props = {
+ name: 'field',
+ form: undefined,
+ modelValue: 'initial'
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'updated'
+ expect(mockContext.emit).toHaveBeenCalledWith('update:modelValue', 'updated')
+ })
+ })
+
+ describe('Nested Property Access', () => {
+ it('should read existing nested properties', () => {
+ const props = {
+ name: 'nested.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBe('nested_value')
+ })
+
+ it('should write to existing nested properties', () => {
+ const props = {
+ name: 'nested.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'updated_nested'
+ expect(mockForm.nested.field).toBe('updated_nested')
+ })
+
+ it('should create missing intermediate objects in nested paths', () => {
+ const props = {
+ name: 'address.street.number',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = '42'
+
+ expect(mockForm.address).toBeDefined()
+ expect(mockForm.address.street).toBeDefined()
+ expect(mockForm.address.street.number).toBe('42')
+ })
+
+ it('should handle multiple levels of nested paths', () => {
+ const props = {
+ name: 'level1.level2.level3.level4.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'deep_value'
+
+ expect(mockForm.level1.level2.level3.level4.field).toBe('deep_value')
+ })
+
+ it('should replace non-object values in nested paths', () => {
+ mockForm.path = 'string_value' // Non-object value
+
+ const props = {
+ name: 'path.to.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'new_value'
+
+ expect(mockForm.path.to.field).toBe('new_value')
+ })
+
+ it('should handle null values in path and replace with objects', () => {
+ mockForm.nullable = null
+
+ const props = {
+ name: 'nullable.nested.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'value'
+
+ expect(mockForm.nullable.nested.field).toBe('value')
+ })
+ })
+
+ describe('Form Prefix Key', () => {
+ it('should prepend formPrefixKey to property names', () => {
+ const props = {
+ name: 'field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const options = {
+ formPrefixKey: 'prefix.'
+ }
+
+ mockForm.prefix = { field: 'prefixed_value' }
+ const { compVal } = useFormInput(props, mockContext, options)
+
+ expect(compVal.value).toBe('prefixed_value')
+ })
+
+ it('should combine formPrefixKey with nested paths', () => {
+ const props = {
+ name: 'nested.field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const options = {
+ formPrefixKey: 'meta.'
+ }
+
+ const { compVal } = useFormInput(props, mockContext, options)
+ compVal.value = 'value'
+
+ expect(mockForm.meta.nested.field).toBe('value')
+ })
+ })
+
+ describe('Validation Error Handling', () => {
+ it('should clear validation errors when value is set', () => {
+ mockForm.errors.has = vi.fn(() => true)
+
+ const props = {
+ name: 'simple_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'new_value'
+
+ expect(mockForm.errors.clear).toHaveBeenCalledWith('simple_field')
+ })
+
+ it('should not attempt to clear errors when form has no errors object', () => {
+ const formWithoutErrors = { simple_field: 'value' }
+
+ const props = {
+ name: 'simple_field',
+ form: formWithoutErrors,
+ modelValue: undefined
+ }
+
+ // Should not throw
+ const { compVal } = useFormInput(props, mockContext)
+ expect(() => {
+ compVal.value = 'new_value'
+ }).not.toThrow()
+ })
+
+ it('should detect form has validation when errors object exists', () => {
+ const props = {
+ name: 'field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { hasValidation } = useFormInput(props, mockContext)
+ expect(hasValidation.value).toBe(true)
+ })
+
+ it('should detect no validation when errors object does not exist', () => {
+ const formWithoutErrors = { simple_field: 'value' }
+
+ const props = {
+ name: 'simple_field',
+ form: formWithoutErrors,
+ modelValue: undefined
+ }
+
+ const { hasValidation } = useFormInput(props, mockContext)
+ expect(hasValidation.value).toBe(false)
+ })
+
+ it('should detect error state correctly', () => {
+ mockForm.errors.has = vi.fn((name) => name === 'simple_field')
+
+ const props = {
+ name: 'simple_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { hasError } = useFormInput(props, mockContext)
+ expect(hasError.value).toBe(true)
+ })
+ })
+
+ describe('Edge Cases', () => {
+ it('should handle empty path strings', () => {
+ const props = {
+ name: '',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ // Empty path will try to get the root form object, which lodash _get handles gracefully
+ // The value will be undefined since the root form object is never set as a value
+ expect(compVal.value).toBeUndefined()
+ })
+
+ it('should handle paths with trailing dots', () => {
+ const props = {
+ name: 'nested.field.',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ // Should handle gracefully without error
+ const { compVal } = useFormInput(props, mockContext)
+ expect(() => {
+ compVal.value = 'value'
+ }).not.toThrow()
+ })
+
+ it('should handle undefined form gracefully', () => {
+ const props = {
+ name: 'field',
+ form: undefined,
+ modelValue: 'default'
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBe('default')
+ })
+
+ it('should handle null form gracefully', () => {
+ const props = {
+ name: 'field',
+ form: null,
+ modelValue: 'default'
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBe('default')
+ })
+
+ it('should handle reading from non-existent nested path', () => {
+ const props = {
+ name: 'does.not.exist',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ expect(compVal.value).toBeUndefined()
+ })
+
+ it('should handle setting boolean values', () => {
+ const props = {
+ name: 'toggle_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = true
+ expect(mockForm.toggle_field).toBe(true)
+ })
+
+ it('should handle setting zero and empty string values', () => {
+ const props = {
+ name: 'numeric_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+
+ compVal.value = 0
+ expect(mockForm.numeric_field).toBe(0)
+
+ compVal.value = ''
+ expect(mockForm.numeric_field).toBe('')
+ })
+
+ it('should handle array values', () => {
+ const props = {
+ name: 'array_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ const testArray = [1, 2, 3]
+ compVal.value = testArray
+
+ expect(mockForm.array_field).toEqual(testArray)
+ })
+
+ it('should handle object values', () => {
+ const props = {
+ name: 'object_field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ const testObject = { a: 1, b: 2 }
+ compVal.value = testObject
+
+ expect(mockForm.object_field).toEqual(testObject)
+ })
+ })
+
+ describe('Integration Scenarios', () => {
+ it('should handle rapid consecutive updates', () => {
+ const props = {
+ name: 'field',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+
+ compVal.value = 'value1'
+ compVal.value = 'value2'
+ compVal.value = 'value3'
+
+ expect(mockForm.field).toBe('value3')
+ expect(mockContext.emit).toHaveBeenCalledTimes(3)
+ })
+
+ it('should maintain form reference across multiple field updates', () => {
+ const props1 = {
+ name: 'field1',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const props2 = {
+ name: 'field2',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal: compVal1 } = useFormInput(props1, mockContext)
+ const { compVal: compVal2 } = useFormInput(props2, mockContext)
+
+ compVal1.value = 'value1'
+ compVal2.value = 'value2'
+
+ expect(mockForm.field1).toBe('value1')
+ expect(mockForm.field2).toBe('value2')
+ })
+
+ it('should handle complex nested update scenario', () => {
+ const props = {
+ name: 'user.address.details.apartment.number',
+ form: mockForm,
+ modelValue: undefined
+ }
+
+ const { compVal } = useFormInput(props, mockContext)
+ compVal.value = 'Apt 5B'
+
+ expect(mockForm.user.address.details.apartment.number).toBe('Apt 5B')
+ expect(mockContext.emit).toHaveBeenCalledWith('update:modelValue', 'Apt 5B')
+ })
+ })
+})
+