Enhance Error Handling and Validation Feedback in Form Components
- Updated error handling in `EditSubmissionModal.vue`, `OpenCompleteForm.vue`, and `OpenForm.vue` to utilize a new `formValidationError` method for improved user feedback on validation issues. - Introduced `formValidationError` function in `useAlert.js` to format and display validation errors more effectively, including detailed error messages for multiple fields. - These changes aim to provide clearer and more actionable feedback to users when form submissions fail due to validation errors, enhancing the overall user experience.
This commit is contained in:
44
client/composables/useAlert.js
vendored
44
client/composables/useAlert.js
vendored
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user