Home › Forums › Front-end Issues › Validate value
Hi there,
So in the docs for validate_value.
My custom validation is working, but for the following:
The custom validation result in the docs seems to look like this:
I’m not getting this for my validation. Mine takes you to another page to display the validation result message, this takes you out of the flow of the form which is not good.
Can anyone tell me why this is happening?
Thanks
It is happening because there is a PHP or JavaScript error happening during the AJAX request that is made to do the validation. You’ll need to look for any JS errors and you’ll also need to turn on error reporting an logging in WP and then look at the error log to see what’s going on. https://codex.wordpress.org/WP_DEBUG
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?
There is no way of knowing. More than likely you’re interfering with the normal operation of the other plugin in some way.
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>
You are attempting to enqueue the scripts after page output has started. Also, since acf_form_head() is not called that I can see acf is unable to process anything that is submitted. Calling acf_form_head() before output starts would do the enqueuing that you are attempting to do yourself. It is impossible to make acf_form() work without acf_form_head() and this must be called before any html output has started.
I would try the following, it’s just rough and I don’t know if it will work. You need to call acf_form_head() at a point that get_current_screen() is available
add_action('admin_init', 'my_admin_acf_form_head', 20);
function my_admin_acf_form_head() {
$screen = get_curretn_screen();
if ($screen->id == 'your screen id') {
acf_form_head();
}
}
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.
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.
It needs to be in a file that will always be loaded every time WP loads, like functions.php. In the main plugin file. Anywhere that code is that will always be executed no matter what else is going on.
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;
}
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.
this
add_action( 'admin_menu', 'sitepoint_settings_page' );
should be
add_action( 'acf/init', 'sitepoint_settings_page' );
and since you are calling acf_form_head(), this should not be necessary
do_action('acf/input/admin_head'); // Add ACF admin head hooks
do_action('acf/input/admin_enqueue_scripts'); // Add ACF scripts
Sorry, error in the above about the action
That is okay, but this
setup_options();
should be
add_action('acf/init', 'setup_options');
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;
}
sorry haven’t replied, been busy.
I have been doing some investigating on this and for some reason the ajax validation request is returning a page and all of it’s html instead of calling the ajax validation ajax function. I’m very confused.
In wp-settings.php line 526
do_action( 'init' );
when this line is run WP returns an html page for some reason???
So more investigating. This is what is being returned by the validation call when the validation failed
<h2>Validation failed</h2><ul><li>testing is not allowed</li></ul>
This should be a JSON encoded value and not an HTML string… still digging.
I have a solution, it was really obvious when my subconscious popped the answer into my consciousness 😛
function add_acf_variables() {
if (!defined('DOING_AJAX') || !DOING_AJAX) {
acf_form_head();
}
}
Actually, I should have figured this out ages ago. You’re not the first person to have issues when adding acf_form_head() on a hook rather than in a template file.
The topic ‘Validate value’ 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.