Support

Account

Forum Replies Created

  • John, you are on FIRE!!!

    That solved it! Thank you SOOOOO much!

  • Does the above update work for you John?

  • 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;
    }
  • Hey John, any thoughts on the previous post? Is there something obvious I’m doing wrong? I’ve tested this out on a couple WordPress instances, and they all had the same issue.

  • First of all John, thank you so much for your time! I really appreciate it.

    To demonstrate my issue I created a simple, to the point single file plugin that demonstrates my issue. With this, when I enter the term ‘testing’ into the ACF field on the ‘ACF ValidationTest’ plugin setting page it will failed validation and I’ll get a separate page that will display the validation message “testing is not allowed”.

    Hope this helps.

    <?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
    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;
    }
    
  • Is there a proper location to be running:

    add_filter('acf/validate_value/name=table_name', 'hc_datatable_name_validation', 10, 4);

    I saw a similar issue mentioned on StackExchange.
    https://wordpress.stackexchange.com/questions/307794/error-in-validate-field-with-acf-plugin-in-wordpress

    Based on that I’ve tried the add_filter at a number of different locations in the code.

  • Sadly that isn’t it. I was already running acf_form_head() and I threw an error_log with both that call and the actual page render code to be sure the head was being called first. It was. I also moved the do_action commands into the function that called the data.php (in my example above) prior to the require_once call.

    The form IS saving properly, it’s just that I can’t do custom validation.

  • Hmmm, I’m not approaching things that differently. What functionality might I be interfering with?

    I have an add_action that runs on ‘admin_menu’. In the callback it fires

    add_submenu_page( 'hc-settings', 'Data Tables', 'Data Tables', 'manage_options', 'hc-data', array($this, 'hc_page_data') );

    Which calls

    function hc_page_data() {
    		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/data.php';
    	}

    The data.php that is inlcuded is

    <div class="wrap">
    	<h2>Home Calculator - Data Tables</h2>
    
    	<div>
    	<?php
    
    	do_action('acf/input/admin_head'); // Add ACF admin head hooks
    	do_action('acf/input/admin_enqueue_scripts'); // Add ACF scripts
    
    	$options = array(
    	    'id' => 'acf-form',
    	    'post_id' => 'options',
    	    'new_post' => false,
    	    'field_groups' => array( 'group_5d448ead2920a' ),
    	    'return' => admin_url('admin.php?page=hc-data'),
    	    'submit_value' => 'Update',
    	);
    	acf_form( $options );
    
    	?>
    	</div>
    </div>
  • I’m running into the same issue but I’m not getting any PHP errors in the log. I’m using the ACF form on a plugin settings page. When the form is submitted before the eventual page refresh it tries to call the same admin URL that it was on but this time with the form POST data. I’m getting a 500 on that call yet no errors go into the server PHP log.

    Is there something unique to this case where I’m using it for an settings page in a custom plugin?

Viewing 9 posts - 1 through 9 (of 9 total)