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.