Skip to content

Beautiful Open-Source Form Builder

Enhance Error Handling and Validation Feedback in Form Components

Julien Nahum0d9c658638f6
diff --git a/client/components/open/components/EditSubmissionModal.vue b/client/components/open/components/EditSubmissionModal.vue
index 5b46cc755..90b6308f1 100644
--- a/client/components/open/components/EditSubmissionModal.vue
+++ b/client/components/open/components/EditSubmissionModal.vue
@@ -61,6 +61,9 @@ const updateForm = (form, onFailure) => {
})
.catch((error) => {
console.error(error)
+ if (error?.data) {
+ useAlert().formValidationError(error.data)
+ }
loading.value = false
onFailure()
})
diff --git a/client/components/open/forms/OpenCompleteForm.vue b/client/components/open/forms/OpenCompleteForm.vue
index d371eae54..d3bd24215 100644
--- a/client/components/open/forms/OpenCompleteForm.vue
+++ b/client/components/open/forms/OpenCompleteForm.vue
@@ -340,8 +340,8 @@ export default {
}
}).catch((error) => {
console.error(error)
- if (error.response && error.data && error.data.message) {
- useAlert().error(error.data.message)
+ if (error.response && error.data) {
+ useAlert().formValidationError(error.data)
}
this.loading = false
onFailure()
diff --git a/client/components/open/forms/OpenForm.vue b/client/components/open/forms/OpenForm.vue
index ff22ff2da..97706fcd7 100644
--- a/client/components/open/forms/OpenForm.vue
+++ b/client/components/open/forms/OpenForm.vue
@@ -456,7 +456,11 @@ export default {
opnFetch('/forms/' + this.form.slug + '/submissions/' + this.form.submission_id).then((data) => {
return {submission_id: this.form.submission_id, id: this.form.submission_id, ...data.data}
}).catch((error) => {
- useAlert().error(error?.data?.message || 'Something went wrong')
+ if (error?.data?.errors) {
+ useAlert().formValidationError(error.data)
+ } else {
+ useAlert().error(error?.data?.message || 'Something went wrong')
+ }
return null
})
)
@@ -628,8 +632,8 @@ export default {
},
handleValidationError(error) {
console.error(error)
- if (error?.data?.message) {
- useAlert().error(error.data.message)
+ if (error?.data) {
+ useAlert().formValidationError(error.data)
}
this.dataForm.busy = false
},
diff --git a/client/composables/useAlert.js b/client/composables/useAlert.js
index b294ff140..2de068e98 100644
--- a/client/composables/useAlert.js
+++ b/client/composables/useAlert.js
@@ -54,6 +54,47 @@ export function useAlert () {
})
}
+ function formValidationError(error, autoClose = 10000, options = {}) {
+ if (!error || !error.errors) {
+ return error(error.message || 'An unknown validation error occurred', autoClose, options)
+ }
+
+ // Count total errors
+ const errorCount = Object.keys(error.errors).length
+
+ // Format error messages as HTML
+ let description = ''
+ if (Object.keys(error.errors).length > 0) {
+ const errorLines = Object.entries(error.errors)
+ .map(([field, messages]) => {
+ // Format each message
+ const formattedMessages = messages.map(message => {
+ return `<li>${message}</li>`
+ });
+
+ return formattedMessages.join('')
+ })
+ .join('')
+
+ description = `<ul class="list-disc pl-4">${errorLines}</ul>`
+ }
+
+ // Add count of errors to the title
+ const title = options.title || (errorCount > 1
+ ? `Validation Error (${errorCount} fields)`
+ : 'Validation Error')
+
+ return useToast().add({
+ icon: 'i-heroicons-x-circle',
+ title,
+ description,
+ color: 'red',
+ timeout: autoClose,
+ html: true,
+ ...options
+ })
+ }
+
function remove (id) {
useToast().remove(id)
}
@@ -63,6 +104,7 @@ export function useAlert () {
error,
warning,
confirm,
- remove
+ remove,
+ formValidationError
}
}