Skip to content

Beautiful Open-Source Form Builder

Implement password error handling in form components (#984)

Chirag Chhatralae2063e72ec48
diff --git a/client/components/open/forms/OpenCompleteForm.vue b/client/components/open/forms/OpenCompleteForm.vue
index df217a53e..5e17b233a 100644
--- a/client/components/open/forms/OpenCompleteForm.vue
+++ b/client/components/open/forms/OpenCompleteForm.vue
@@ -388,8 +388,21 @@ const editSubmission = () => {
window.parent.location.href = editUrl
}
+const addPasswordError = (msg) => {
+ passwordForm.errors.set('password', msg)
+}
+
+// Inject password error from parent
+const passwordError = inject('passwordError', ref(null))
+
+// Watch for password error from parent and display it
+watch(passwordError, (errorMsg) => {
+ if (errorMsg) {
+ addPasswordError(errorMsg)
+ }
+}, { immediate: true })
+
const passwordEntered = () => {
- console.log('passwordEntered', passwordForm.password)
if (passwordForm.password) {
emit('password-entered', passwordForm.password)
} else {
@@ -397,12 +410,7 @@ const passwordEntered = () => {
}
}
-const addPasswordError = (msg) => {
- passwordForm.errors.set('password', msg)
-}
-
defineExpose({
- addPasswordError,
restart,
formManager
})
diff --git a/client/components/open/forms/OpenForm.vue b/client/components/open/forms/OpenForm.vue
index cd9ab5a30..efa7f6505 100644
--- a/client/components/open/forms/OpenForm.vue
+++ b/client/components/open/forms/OpenForm.vue
@@ -183,6 +183,8 @@ const formPageIndex = computed(() => props.formManager.state.currentPage)
const strategy = computed(() => props.formManager.strategy.value)
const structure = props.formManager.structure
+// Slots/utilities
+const slots = useSlots()
const hasPaymentBlock = computed(() => structure.value?.currentPageHasPaymentBlock?.value ?? false)
@@ -242,7 +244,7 @@ const isProcessing = computed(() => props.formManager.state.isProcessing)
const isSubmitted = computed(() => !!props.formManager?.state.isSubmitted)
const shouldDisplayForm = computed(() => {
const showAdminControls = !!props.formManager?.strategy?.value?.admin?.showAdminControls
- return (!isSubmitted.value && !form.value?.is_closed && !form.value?.max_number_of_submissions_reached) || showAdminControls
+ return (!isSubmitted.value && !form.value?.is_closed && !form.value?.max_number_of_submissions_reached && !slots.password) || showAdminControls
})
const submittedData = computed(() => props.formManager?.form?.data?.() ?? null)
diff --git a/client/pages/forms/[slug]/index.vue b/client/pages/forms/[slug]/index.vue
index 86742e1ce..cbf7a8e92 100644
--- a/client/pages/forms/[slug]/index.vue
+++ b/client/pages/forms/[slug]/index.vue
@@ -59,7 +59,10 @@ if (import.meta.server) {
await suspense()
}
-const openCompleteFormRef = ref(null)
+const passwordError = ref(null)
+
+// Provide password error state for child component
+provide('passwordError', passwordError)
const passwordEntered = function (password) {
const cookie = useCookie('password-' + slug, {
@@ -68,17 +71,15 @@ const passwordEntered = function (password) {
secure: true
})
cookie.value = sha256(password)
+
+ // Clear any previous error
+ passwordError.value = null
+
nextTick(() => {
refetchForm().then(() => {
if (form.value?.is_password_protected) {
- // Add another nextTick to ensure the component is fully rendered after refetch
- nextTick(() => {
- if (openCompleteFormRef.value && typeof openCompleteFormRef.value.addPasswordError === 'function') {
- openCompleteFormRef.value.addPasswordError(t('forms.invalid_password'))
- } else {
- console.warn('openCompleteFormRef ref not available or addPasswordError method not found')
- }
- })
+ // Set error message - child component will pick it up
+ passwordError.value = t('forms.invalid_password')
} else {
trackFormView()
}