Home › Forums › Front-end Issues › acf_form “Validation Failed” is followed by wp_die() so breaks the website › Reply To: acf_form “Validation Failed” is followed by wp_die() so breaks the website
The code you mentioned in includes/validation.php outputs the errors and then calls wp_die(), which terminates the script execution and displays an error message. This behavior can cause the site to not finish loading properly.
To address this issue, you can modify the validation code to handle the empty required fields more gracefully. Instead of using wp_die(), you can redirect the user back to the form page and display the errors there, allowing them to correct the form without breaking the site.
Here’s an example of how you can modify the validation code to achieve this:
php
function handle_acf_form_submission() {
if (isset($_POST[‘acf_nonce’]) && isset($_POST[‘acf_field_key’])) {
// Verify the ACF form nonce
if (!wp_verify_nonce($_POST[‘acf_nonce’], ‘acf_form_nonce’)) {
// Nonce verification failed, handle accordingly
} else {
// Get the ACF field key
$field_key = $_POST[‘acf_field_key’];
// Check if the required field is empty
if (empty($_POST[$field_key])) {
// Field is empty, store the error message
$error_message = ‘Please fill in the required field.’;
// Redirect back to the form page with the error message
wp_redirect(add_query_arg(‘error’, urlencode($error_message), get_permalink()));
exit();
}
// Process the form submission
// …
}
}
}
add_action(‘init’, ‘handle_acf_form_submission’);
In this example, the code checks if the required field is empty. If it is, it stores an error message and redirects the user back to the form page with the error message appended as a query parameter. The user can then see the error message and correct the form submission without breaking the site.
Make sure to adjust the code to match your specific implementation, including the form field names, error handling, and redirection logic.
By implementing this modification, the site should handle empty required fields more gracefully and avoid breaking when submitting the ACF form on the front end.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.