Home › Forums › Front-end Issues › Front-end acf_form: field validation returning a WP error page
I’ve created an ACF form for the front end with a couple of options: one set of checkbox inputs, and another set of radio buttons. I’ve made the checkbox selection required (so at least one has to be checked); however, the Validation failed message returns me to the same URL, but rendered as a WP error page:
Am I missing something, or is this expected behaviour? I have a handful of other plugins, but even disabling everything except ACF and the custom one required to render the page with the form on still results in this style of error. I’d much prefer it to render the actual page with that error in it, rather than replace the page entirely.
When this happens in generally means that there was a php error or warning generated during the validation ajax request. Turn on error debugging and error logging and check your error log. https://codex.wordpress.org/Debugging_in_WordPress
I’m not sure that’s the case: the function validate_save_post
in /core/validation.php
will wp_die()
with the validation message if the $show_errors
parameter is set to true, which is exactly what I’m getting above.
Doing a manual stack trace, it looks like it’s being called from save_user()
in /forms/user.php
, wherein it checks if acf_validate_save_post(true)
returns true, then save the user info. That parameter is $show_errors
, and is hard-set.
For the sake of a sanity check, I enabled WP_DEBUG and all I got were a couple of notices about an undefined index in my functions file. I amended those and still got the same result, but having looked at the code, it looks like this is how it’s supposed to behave?
Just to be clear: I’m using acf_form()
on the front end to update user details. There are a couple of forms on the same page, and they all work – whenever I submit a form, I get the little spinner, then the page refreshes with the query_vars
specified in my acf_form()
as $_GET
arguments. It’s just that when one of the required fields is left blank, I end up with this ugly wp_die()
screen instead of either some ajax response or an in-form error message.
For reference, here’s my code:
$userObject = wp_get_current_user();
$userID = $userObject->ID;
$profileOptions = array(
'post_id' => 'user_' . $userID,
'id' => 'profile-form',
'field_groups' => array('vnm_profile__acf_group_user_profile'),
'form' => false,
'return' => add_query_arg(array('section-updated' => 'profile', 'updated' => 'true'), get_permalink()),
'updated_message' => __('Profile information updated!', 'my-theme'),
'submit_value' => __('Update Profile', 'my-theme'),
);
acf_form($profileOptions);
You should open a new support ticket for this one https://support.advancedcustomfields.com/new-ticket/
I’ve emailed Elliot, but it looks like a function in my theme to defer JS loading is the culprit that prevents acf_form()
from making a call to admin-ajax.php
for some reason:
if (!is_admin()) {
add_filter('clean_url', function($url) {
if (strpos($url, '.js') === false || strpos($url, 'jquery.min.js') != false || is_page('my-account')) {
return $url;
}
return "$url' defer='defer";
}, 11, 1);
}
This adds defer="defer"
to all enqueued JS files (apart from jQuery). If I comment this out, the ACF form does ajax-based inline validation. For now I’ve added to the conditional statement so that it doesn’t defer scripts when on the page containing the form.
So basically, the ajax validation is not happening. When the ajax validation fails for some reason then there is a php falback that validates the fields and shows the error you’re seeing if anything fails validation.
Looks that way, yep. However, as this was my first time using acf_form()
and all I’d seen it do was full-on page refreshes/redirects, I assumed that’s how it worked – I had no idea it was supposed to ajax-validate the form elements.
Hi indextwo,
I’m fighting with the same the same behavior which happens to all users with role != admin.
How did you solve it? I don’t get your answer from your reply which is marked as topic solution.
Thanks for help
I believe the issue I had was that, in my theme’s functions.php
file, I was deferring my JS files. As such, the inline JS vars set by ACF when acf_form()
is used weren’t available to the scripts at page load. I added an exception to not defer any scripts with acf
in their filename, and that solved the issue for me.
Hi,
i am facing the same issue , Can you please share your code “I added an exception to not defer any scripts with acf in their filename”
or do you have a solution to include them manually after the form ?
i really appreciate it
Thank …
Same issue here.
I get the error page for all non-admin users
(it’s working great for admins)
Couldn’t find any “defer” that coud explain the behavior.
Any help ?
I have the same issue here did anyone find a way to fix it?
In my functions.php
file, I added the following:
if (!is_admin()) {
add_filter('clean_url', function($url) {
if (strpos($url, '.js') === false || strpos($url, 'jquery.min.js') != false || strpos($url, 'acf') != false) {
return $url;
}
return "$url' defer='defer";
}, 11, 1);
}
The clean_url
filter modifies the URL of any script being enqueued – this is where I was adding my own defer
property to my scripts. However, as this was causing an issue with my ACF forms, I added an exception so that it didn’t add a defer
for any ACF scripts.
If you’ve got something else that’s handling script deferment, you’ll need to check to see if it has hooks that allow you to add exceptions.
The topic ‘Front-end acf_form: field validation returning a WP error page’ 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.