Home Forums Front-end Issues acf_form “Validation Failed” is followed by wp_die() so breaks the website


acf_form “Validation Failed” is followed by wp_die() so breaks the website

  • in includes/validation.php you have this code below. What is happening is when I save an acf_form on the front end and we submitted an empty required field, then as you can see below, the errors are output and then it’s wp_die(). So the site breaks as it does not get finished loading properly.

    I found another post about this, but it was all about the form using Ajax. I dont believe my forms use Ajax, they just get submitted back to the same page in a normal POST request.


    function acf_validate_save_post( $show_errors = false ) {
    	// action
    	do_action( 'acf/validate_save_post' );
    	// vars
    	$errors = acf_get_validation_errors();
    	// bail early if no errors
    	if ( ! $errors ) {
    		return true;
    	// show errors
    	if ( $show_errors ) {
    		$message  = '<h2>' . __( 'Validation failed', 'acf' ) . '</h2>';
    		$message .= '<ul>';
    		foreach ( $errors as $error ) {
    			$message .= '<li>' . $error['message'] . '</li>';
    		$message .= '</ul>';
    		// die
    		wp_die( $message, __( 'Validation failed', 'acf' ) );
    	// return
    	return false;
  • Do you have any custom field validation using acf/validate_value?

    What is happening and why are you seeing this error?

    ACF attempts to validate the fields using AJAX before the form is submitted. During this validation an error occurs, usually a PHP error, but could any number of things. Whatever is happening is preventing ACF from sending the correct response to the validation AJAX request. When this happens ACF submits the form and fields are re-validated using PHP. An error in field input or in custom validation filters is causing the form submission to fail.

    The solution is to track down the error that is causing the AJAX Request to fail. This generally means enabling error logging in WP and looking in the error log to find the error and correct it.

  • I’m sorry I should have said…

    There are no WordPress or php errors with debug enabled or in the server error log.

    There are no console errors, I can’t see Ajax even being called.

    The validation works, because it does show the error “Categories is required”. Thats why we get the error because a required field is left blank that’s all.


  • As I said, there are other reasons this could be happening.

    One such other reason is that something is causing output before ACF sends its response to the ajax request. This could be as simple as white-space being output either before a <?php tag or after a ?> tag. This is the same type of thing that causes the PHP Warning

    Warning: Cannot modify header information - headers already sent by ......
  • Ok thanks. Ajax was not even called before though. But no console errors of other JS that would stop JS from working. Anyway….

    Today I went to look into it and its magically fixed itself and its using ajax as it should and shows the error on the same page fine ヽ(ಠ_ಠ)ノ

  • 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:


    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()));

    // 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.

  • Thats great, thank you.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.