I am using this function to validate 2 fields.
function pg_check_values( $valid, $value, $field, $input ) {
// bail early if value is already invalid
if ( ! $valid ) {
return $valid;
}
if ( 1 == $value ) {
if ( isset( $_GET[ 'id' ] ) ) {
if ( DpgPostTypes::LONGREAD == get_post_type( $_GET[ 'id' ] ) ) {
$start_date = get_field( 'pg_start_date', $_GET[ 'id' ] );
if ( false == $start_date ) {
$valid = __( 'Please enter a start date', 'dpg' );
}
$show_on_title = get_field( 'pg_domain_selector', $_GET[ 'id' ] );
if ( false == $show_on_title ) {
$valid = __( 'Please select on which domains this longread should \'run\'', 'dpg' );
}
}
}
}
return $valid;
}
add_filter( 'acf/validate_value/name=pg_ready_to_check', 'pg_check_values', 10, 4 );
This is what I see: https://imgur.com/a/JuduehI
But I expected to see something like explained in the validate_value info.
Anyone know what I’m doing ‘wrong’ ??
This usually happens when there is an error during the AJAX request. Check for PHP errors during the request.
In your function you are using get_field() to get values. ACF has not saved any values during validation. When validating fields you need to look in the $_POST[‘acf’] array (or $_POST[‘fields’] if your using ACF4).
I just remembered I fixed this issue and still had this open.
This is my solution:
function pg_check_values( $valid, $value, $field, $input ) {
// bail early if value is already invalid
if ( ! $valid ) {
return $valid;
}
if ( 1 == $value ) {
if ( isset( $_POST[ 'acf' ][ 'field_5adf64026e9a8' ] ) && empty( $_POST[ 'acf' ][ 'field_5adf64026e9a8' ] ) ) {
$valid = __( 'Please enter a start date', 'dpg' );
}
}
return $valid;
}
add_filter( 'acf/validate_value/name=pg_ready_to_check', 'pg_check_values', 10, 4 );
The topic ‘Validation looks 'unexpected'’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.