Custom Error Handling in Forminator Forms
-
Hi,
I’m trying to add backend functionality to my Forminator form. Specifically, I want to add an entry to a database when a user clicks the submit button on my Forminator form. I have already managed to do this successfully. However, I’m encountering an issue: if the database insertion fails (which can happen for various reasons), I want to notify the user of this error and prevent the Forminator post from proceeding (e.g., stop the emails configured in Forminator from being sent).
Here is the code I have so far:
add_action(
"forminator_form_after_handle_submit",
"on_forminator_form_submit",
10,
2,
);
add_action(
"forminator_form_ajax_submit_response",
"on_forminator_form_submit",
10,
2,
);
function on_forminator_form_submit($response, $form_id) {
if ($response && is_array($response)) {
if ($response["success"]) {
if ($form_id == 963) {
// Retrieve the content of the Forminator form
// Insert data into the database
// If the database insertion is successful
if (db_insertion_successful()) {
return $response;
} else {
// Return a custom error message to display in the form
// Prevent the emails configured in Forminator from being sent
// TODO: Implement this part
}
}
}
}
return $response;
}My main issue is that I don’t know how to implement the part where it says “TODO”. I need to return a custom error message to the user and stop the Forminator emails from being sent if the database insertion fails.
I would greatly appreciate any help with this. Unfortunately, there is very little documentation available on using hooks in Forminator.
Thanks!
- You must be logged in to reply to this topic.