Error in Validate Field with ACF plugin in Wordpress

I have the following problem when I am validating a field with the Advanced Custom Fields plugin in wordpress. What happens is that the field is validated correctly but the error appears on a new page instead of going out on the same page above the field to which I am validating. The code to validate is the following:

function validate_fields_contact()
{
    add_filter('acf/validate_value/name=phone_contact', 'validate_phone_number', 10, 4);
}

function validate_phone_number($valid, $value, $field, $input)
{
    if (!$valid) {
        return $valid;
    }

    if(!preg_match("/^\+XX(\s|\d){8,12}$/", $value)) {
        return __('Incorrect Format.');
    }

    return true;
}

It should be like that:

This is what happens

Topic advanced-custom-fields 500-internal-error validation Wordpress

Category Web


Two days searching for the fix to the same problem... It looks like the acf_form_head() is not necessary in the validation and the error, in my case, is there. So I add this if and the magic happened.

function acf_inscs_head () {
    if ( !wp_doing_ajax() ) // avoid to load acf_form_head() on validation
        acf_form_head();
}
add_action( 'init', 'acf_inscs_head', -1 );

If anyone stumbles upon this later -- in my case it was because I had an admin_init hook to prevent access to wp-admin for a certain role (which is why we usually have a front end ACF form anyway right?) and it would produce an error when trying to run admin-ajax.php. ACF uses that file for AJAX validation so when you run some sort of "prevent wp-admin" hook, that stops ACF validation, too.

So, the hook interfering was:

add_action( 'admin_init', 'custom_no_admin_access', 100 );
function custom_no_admin_access() {
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    if($user_role === 'special-role'){
        exit( wp_redirect(home_url()));
    }
}

and I turned it into:

add_action( 'admin_init', 'custom_no_admin_access', 100 );
function custom_no_admin_access() {
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    if($user_role === 'special-role' && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php'){
        exit( wp_redirect(home_url()));
    }
}

A big help for troubleshooting was using Chrome's Inspector to track the network requests. Go to the Network tab and check "Preserve Log" so you can see the history of requests. Try to submit the form and I could see it was redirecting to the home page (which my admin_init hook says to do when attempting to use admin-ajax.php) before failing the validation.

Hope that helps!


I did have the same issue. And I wasted enough time for the answer.

At first be sure that:

The ajax request isn't failed and happens. So, check:

  1. Is acf_form_head() before get_header() and run before any html is output?
  2. Does your theme contain call to wp_head()?
  3. Does your theme contain call to wp_foot()?
  4. Are your deferring the loading of JavaScript or otherwise altering the way that JavaScript is loaded on the page?

(Look at this ACF support topic too. If you use acf_form() for creating new user look at this topic).

But in my case the root was is_admin() in this line into the 'acf/validate_value' hook:

if ( ! $valid || is_admin() ) { return $valid; }

Because is_admin() returns 'true' by AJAX requests. As a result, the validate function didn't work.

Hope it will helpfully for somebody.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.