Search code examples
wordpressadvanced-custom-fields

Word Press: How to add custom validation to Advanced Custom Fields


I'm trying to use this plug in: Advanced Custom Fields and add my own validation for specific fields.

I tried to follow this post:

https://www.advancedcustomfields.com/resources/acf-validate_value/

But what file do I change in my word press folder to allow it to work?

I'm trying to validate a Pin # that matches 1234.

I have recently tried to put it in the acf plugin folder for validation.php.

I tried it in here:

In

function acf_validate_value( $value, $field, $input ) {

    // vars
    $valid   = true;
    $message = sprintf( __( '%s value is required', 'acf' ), $field['label'] );

    // valid
    if ( $field['required'] ) {

        // valid is set to false if the value is empty, but allow 0 as a valid value
        if ( empty( $value ) && ! is_numeric( $value ) ) {

            $valid = false;

        }
    }

    /**
    *  Filters whether the value is valid.
    *
    *  @date    28/09/13
    *  @since   5.0.0
    *
    *  @param   bool $valid The valid status. Return a string to display a custom error message.
    *  @param   mixed $value The value.
    *  @param   array $field The field array.
    *  @param   string $input The input element's name attribute.
    */
    $valid = apply_filters( "acf/validate_value/type={$field['type']}", $valid, $value, $field, $input );
    $valid = apply_filters( "acf/validate_value/name={$field['_name']}", $valid, $value, $field, $input );
    $valid = apply_filters( "acf/validate_value/key={$field['key']}", $valid, $value, $field, $input );
    $valid = apply_filters( 'acf/validate_value', $valid, $value, $field, $input );
    $valid = add_filter('acf/validate_value/name=registration_pin', 'my_acf_validate_value', 10, 4);

    // allow $valid to be a custom error message
    if ( ! empty( $valid ) && is_string( $valid ) ) {

        $message = $valid;
        $valid   = false;

    }

    if ( ! $valid ) {

        acf_add_validation_error( $input, $message );
        return false;

    }

    // return
    return true;

}

function my_acf_validation_registation_pin($valid, $value, $field, $input_name ){
    // Bail early if value is already invalid.
    if( $valid !== true ) {
        return $valid;
    }

    console.log('validate registration pin: test ->' + new Date());
}

Solution

  • Yeah your code wont work. The add_filter will be hit too late.

    If you are simply trying to get an acf field to validate the submitted value against a defined set value.

    Best way to do this is to add this to your functions.php, see comments in my code below...

    <?php
    
    // add validate value filter for all acf fields with the field name `registration_pin`
    // you can use `acf/validate_value/key=` to target a specific acf field by key if you have multiple fields with the same name `registration_pin` in different field groups
    add_filter('acf/validate_value/name=registration_pin', 'acf_validate_reg_pin', 10, 4);
    
    // function used by the above `add_filter`
    function acf_validate_reg_pin($valid, $value, $field, $input_name) {
    
        // define our registration pin to validate submitted value against
        $reg_pin = '1234';
    
        // if submitted value is empty or false
        // remove this condition if you want the form to still validate if the field value is empty/false
        if(!$value) {
    
            // field returns invalid and show this error message
            return __('Please enter registration pin.');
    
        }
    
        // if submitted value is a string and is not equal to our defined registration pin
        if(is_string($value) && $value !== $reg_pin) {
    
            // field returns invalid and show this error message
            return __('Incorrect registration pin.');
    
        }
    
        // return field as valid, if none of the above conditions are true
        return $valid;
    
    }
    

    This might make it a bit clearer for you about how to handle validation on specific acf fields. You should use this add_filter in your functions.php so the filter is hit.

    You can use require_once(__DIR__ . '/folder/example.php'); in your functions.php, if you want to keep things tidy in your functions. You can load php files, lib classes, etc... everything required here runs first in the wordpress admin and your theme.