Support

Account

Home Forums ACF PRO Validation looks 'unexpected'

Solved

Validation looks 'unexpected'

  • 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).

  • Thanks. Good to know. Will check.

  • 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 );
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.