Advanced Validation

Define custom validation rules and error messages based on the current screen location in the field’s UI. In order to enable the Advanced Validation UI, you must enable the Field Group: Advanced Settings feature.

  • Order
  • Label
  • Name
  • Key
  • Type

Validate value against rules

1
1
1
1
1

Available Locations

Choose the location screen where the validation rule should be applied. You can set multiple rules groups based on different locations.

  • Everywhere
  • Administration
  • Front-end

Available Functions

The UI comes with predefined functions that can be used out of the box.

Function namePHP Function
If valueif($value)
If value lengthif(strlen($value))
If count valueif(count($value))
If email existsif(email_exists($value))
If post type existsif(post_type_exists($value))
If taxonomy existsif(taxonomy_exists($value))
If term existsif(term_exists($value))
If username existsif(username_exists($value))
If is emailif(is_email($value))
If is user logged inif(is_user_logged_in())
If is arrayif(is_array($value))
If is stringif(is_string($value))
If is numericif(is_numeric($value))
If get post typeif(get_post_type($value))
If post id existsif(get_post_by_id($value))
If post slug existsif(get_post_by_slug($value))
If post title existsif(get_post_by_title($value))
If sanitize emailif(sanitize_email($value))
If sanitize file nameif(sanitize_file_name($value))
If sanitize html classif(sanitize_html_class($value))
If sanitize keyif(sanitize_key($value))
If sanitize metaif(sanitize_meta($value))
If sanitize mime typeif(sanitize_mime_type($value))
If sanitize optionif(sanitize_option($value))
If sanitize text fieldif(sanitize_text_field($value))
If sanitize titleif(sanitize_title($value))
If sanitize userif(sanitize_user($value))
If get user by idif(get_user_by('id', $value))
If get user by slugif(get_user_by('slug', $value))
If get user by emailif(get_user_by('email', $value))
If get user by loginif(get_user_by('login', $value))

Available Operators

The UI comes with predefined operators that can be used out of the box.

operator
==
!=
>
>=
<
<=
Contains
Doesn’t contain
Starts with
Doesn’t start with
Ends with
Doesn’t end with
Matches regex
Doesn’t match regex
== true
!= true
== false
!= false
== null
!= null
== “” (empty)
!= “” (empty)

Set Validation Rules in PHP

It is possible to add validation rules based on the screen location programmatically using the acf/validate_value hook combined with acfe_is_admin() and acfe_is_front() functions.

Note that this method doesn’t require to enable the “Advanced Settings” in the field group as it is directly executed in PHP. See ACF documentation. Usage example:

/*
 * https://www.advancedcustomfields.com/resources/acf-validate_value/
 */
add_filter('acf/validate_value/name=my_field', 'my_acf_validate_my_field', 10, 4);
function my_acf_validate_my_field($valid, $value, $field, $input){
    
    // Admin only
    if(acfe_is_admin()){
        
        if(strlen($value) > 10)
            $valid = 'The value should contain less than 10 characters';
        
    }
    
    // Front-end only
    if(acfe_is_front()){
    
        if(strlen($value) > 30)
            $valid = 'The value should contain less than 30 characters';
        
    }
    
    // return
    return $valid;

}