Support

Account

Home Forums Front-end Issues Validate value Reply To: Validate value

  • Thanks for the response!

    Sadly there was no change.

    I changed the setup_options call to be on the acf/init action and I commented out the the two do_actions.

    I’m still getting the separate page with the custom validation message.

    <?php
    
    /**
     * @wordpress-plugin
     * Plugin Name:       ACF Validation Test
     * Description:       ACF Test
     * Version:           1.0.0
     * Author:            Steve
     * License:           GPL-2.0+
     * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
     */
    
    // define ACF group
    add_action('acf/init', 'setup_options');
    
    // Add ACF form head
    add_action( 'admin_init', 'add_acf_variables' );
    
    // Create plugin setting page
    add_action( 'admin_menu', 'sitepoint_settings_page' );
    
    // Listen for 'input_test' form submit
    add_filter('acf/validate_value/name=input_test', 'test_validate_function', 10, 4);
    
    function sitepoint_settings_page() {
        add_menu_page( 'ACF Validation Test', 'ACF Validation Test', 'manage_options', 'acf-validation', 'plugin_settings_page_content' );
    }
    
    function test_validate_function( $valid, $value, $field, $input ) {
    	
    	// bail early if value is already invalid
    	if ( !$valid ) return $valid;
    
    	if ($value == 'testing') {
    		$valid = "testing is not allowed";
    	}
    
    	return $valid;
    }
    
    function add_acf_variables() {
        acf_form_head();
    }
    
    function plugin_settings_page_content() {
    	//do_action('acf/input/admin_head'); // Add ACF admin head hooks
        //do_action('acf/input/admin_enqueue_scripts'); // Add ACF scripts
    
    	echo "<h1>ACF Validation Test</h1>";
    
        $options = array(
            'id' => 'acf-form',
            'post_id' => 'options',
            'new_post' => false,
            'field_groups' => array( 'group_5d5ac7f764f02' ),
            'return' => admin_url('admin.php?page=acf-validation'),
            'submit_value' => 'Update',
        );
        acf_form( $options );
    }
    
    function setup_options() {
        if( function_exists('acf_add_local_field_group') ):
    
    	acf_add_local_field_group(array(
    		'key' => 'group_5d5ac7f764f02',
    		'title' => 'acf test',
    		'fields' => array(
    			array(
    				'key' => 'field_5d5ac7fd07396',
    				'label' => 'input_test',
    				'name' => 'input_test',
    				'type' => 'text',
    				'instructions' => "if defined as 'testing' then validation will fail.",
    				'required' => 0,
    				'conditional_logic' => 0,
    				'wrapper' => array(
    					'width' => '',
    					'class' => '',
    					'id' => '',
    				),
    				'default_value' => "",
    				'placeholder' => '',
    				'prepend' => '',
    				'append' => '',
    				'maxlength' => '',
    			),
    		),
    		'location' => array(
    			array(
    				array(
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'post',
    				),
    			),
    		),
    		'menu_order' => 0,
    		'position' => 'normal',
    		'style' => 'default',
    		'label_placement' => 'top',
    		'instruction_placement' => 'label',
    		'hide_on_screen' => '',
    		'active' => true,
    		'description' => '',
    	));
    
    	endif;
    }