Home › Forums › Front-end Issues › Validate value › Reply To: Validate value
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;
}
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.