0

I want to add custom error handling in acf backend. Here's my code:

function dfg_acf_validate_value( $valid, $value, $field, $input_name ) {

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

    if( $value != '30' and $value != 360 ) {
        return __( 'Please choose a valid type!' );
    }
    return $valid;
}

add_filter('acf/validate_value/name=dfg_service_type', 'dfg_acf_validate_value', 10, 4);

and in the backend when I choose something else (instead of 30 or 360) nothing happens. What's the reason for that?

I actually found that the hook is not fired. What's the reason for that?

function dfg_acf_validate_value( $valid, $value, $field, $input_name ) {
    
        if( $value != '30' ) {
            wp_mail('test@example.com', 'subject', 'message');
        }
        return $valid;
    }
    
    add_filter('acf/validate_value/name=dfg_service_type', 'dfg_acf_validate_value', 10, 4);
6
  • Welcome to wpse! Instead of targeting name try targeting key acf/validate_value/key={$key}, maybe that will trigger the filter Jan 11 at 11:03
  • @Buttered_Toast Thank you, I had tried targeting key and nothing happened. Jan 11 at 11:19
  • Could you add the code that you tried to the question? the filter with key Jan 11 at 11:19
  • @Buttered_Toast Here's the code: add_filter('acf/validate_value/acf/validate_value/key=field_61dd4304ab2c2', 'dfg_acf_validate_value', 10, 4); Jan 11 at 11:40
  • You have acf/validate_value/ twice, should be acf/validate_value/key=field_61dd4304ab2c2 Jan 11 at 11:45

1 Answer 1

0

Lets walk though the acf/validate_value filter logically...

I'm assuming both of your code snippets are the same snippet, and you are not executing them together (as @Buttered_Toast pointed out).

Here is your code (see my added comments)

code below is assuming your dfg_service_type acf field is a number field...

/**
  * @param mixed $valid Whether or not the value is valid (boolean) or a custom error message (string).
  * @param mixed $value The field value.
  * @param array $field The field array containing all settings.
  * @param string $input_name The field DOM element name attribute.
  * @return bool|string
 */
function dfg_acf_validate_value($valid, $value, $field, $input_name) {

    // $valid true by default
    // but if $field['required'] is true then
    // valid is set to false if the value is empty, but allow 0 as a valid value
    if( $valid !== true ) {
        return $valid;
    }

    // set value if value is true and cast value to integer else set bool false
    $value = $value ? (int)$value : false;

    // here is your first snippet cleaned up...
    // you are simply just checking if value is not 30 and not 360
    // if anything but 30 or 360, the below condition will return __( 'Please choose a valid type!' ); 
    // is this your intention?  
    if( $value && ( $value !== 30 && $value !== 360 )) {
        return __( 'Please choose a valid type!' );
    }

    /**
        // here is another option to check if $value is greater than or equal too 30 and less than or equal too 360...
        if( $value && ( $value >= 30 && $value <= 360 )) {
            return __( 'Please choose a valid type!' );
        }
    */    

    // return valid and save value if above value conditions are not run
    return $valid;

}

// init the filter perform custom validation on the field’s `$value` before it is saved into the database.
// for all acf fields with the name `dfg_service_type`
add_filter('acf/validate_value/name=dfg_service_type', 'dfg_acf_validate_value', 10, 4);

Update

Let try and debug this.

Add some debug functions to the top of your functions.php which dumps then exits...

presuming your php version is 5.6+ so variable-length arg lists token works

<?php
/**
 * functions.php
 */

// dump and exit function
function dd(...$args) {
    dump(...$args);
    exit;
}

// dump
function dump(...$args) {
    foreach ($args as $dump) {
        echo '<pre class="dd">' . print_r($dump, true) . '</pre>';
    }
}

Then try this instead and save the field.

function dfg_acf_validate_value($valid, $value, $field, $input_name) {

  // test this is getting hit
  dd($valid, $value, $field, $input_name);    

}

add_filter('acf/validate_value/name=dfg_service_type', 'dfg_acf_validate_value', 10, 4);

If this returns dd results then the field validate value is getting hit.

Let me know how you get on and what is returned, and i'll try help some more.

2
  • Thank you, it still doesn't work. even if for example all the code for dfg_acf_validate_value function is 'if ($value != 1) {return __( 'Please choose a valid type!' );}' it doesn't work. I think the trigger itself is not triggered: acf/validate_value/name=dfg_service_type Jan 12 at 10:52
  • @ArashNaderi see update in my answer.
    – joshmoto
    Jan 15 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.