Well, I’m embarrassed. After reading the ACF source code I’ve found a display_errors
function.
To display the errors messages you just need to add it into the callback of the POST request, like this :
$.post(
my_ajax_obj.ajax_url, data,
function(result) {
errors = result.data.errors
if (errors === 0) {
// Code for success
} else {
acf.validation.display_errors( errors, $("form.acf-form") );
}
}
);
You can do it with acf/validate_save_post
, see my latests posts here : https://support.advancedcustomfields.com/forums/topic/trigger-verification-of-the-form-without-submitting-it/
Add the errors with the proper acf function :
acf.validation.add_error(field_container, errors[i]['message']);
field_container
is the jQuery object of the .acf-field
direct parent of the .acf-input
errors[i]['message']
is the message (see how I get the errors
in my previous post)
A POST request with a good nonce, the action acf/validate_save_post
and the fields data return valuable informations.
{
nonce: acf.o.nonce,
action: 'acf/validate_save_post',
acf[field_id1...]: 'value on the input field_id1...',
acf[field_id2...]: 'value on the input field_id2...'
}
I’ve used it that way :
$("#validation-button").click( function(e) {
e.preventDefault();
validateACFinputs($(this.form).serializeArray());
});
function validateACFinputs(form_data) {
var data = {
nonce: acf.o.nonce,
action: "acf/validate_save_post",
post_id: acf.o.post_id };
for (const i in form_data) {
data[form_data[i].name] = form_data[i].value;
}
$.post(
my_ajax_obj.ajax_url, data,
function(result) {
errors = result.data.errors
if (errors === 0) {
// Code for success
} else {
// Code for fail
}
}
);
}
(my_ajax_obj.ajax_url
contains the url of admin-ajax.php, if you don’t know what it is, please read the WordPress documentation about admin-ajax.php)
Notes :
.checkValidity()
on one of the input, because it seems to cast the whole ACF validation process.checkValidity()
.acf
but I haven’t tried it yet.This remains incomplete compared to the classical behavior when you click on the submit…
I just leave a answer here to say that I would also love to have a way to avoid duplicate ID when multiples acf_form()
are used.
Hello Chris, I try to use acf_reset_validation_errors()
inside acf/validate_save_post
.
I can see the request to admin-ajax.php and it returns {"success":true,"data":{"valid":1,"errors":0}}
but the error messages still appear and the post isn’t saved.
I have set post_status
to draft
and I have this :
add_action('acf/validate_save_post', 'clear_all_errors', 10, 0);
function clear_all_errors() {
acf_reset_validation_errors();
}
Can you share some code about how you did it ?
EDIT : It was because now ACF use the required
on inputs
I’ve added formnovalidate
on my submit button and it solved my issue.
Okay, I’ll try to figure it out later. If I do, I’ll document it here !
I started discussing about it in another topic (Trigger verification of the form without submitting it).
But I don’t have a good solution at the moment.
An inelegant solution would be to watch for ACF errors messages in the current step before displaying the next one.
This solution lack of few ACF functionality. For example an image field will be valid for any type of files.
Okay, thanks for the clear answer.
I’ve used the checkValidity()
JS function.
$('#form-part-1 button.input-submit').click(function(e) {
e.preventDefault();
var valid = true;
$('#form-part-1').find('input').each(function() {
if (valid === true) {
valid = this.checkValidity();
}
});
if (valid === true) {
$('.acf-error-message').remove();
$('#form-part-2').show();
$('#form-part-1').slideUp();
}
});
This isn’t perfect, and it don’t use acf/validate_value
or acf/validate_save_post
, but I can’t manage to use them with the ACF Javascript API
Have you tried to print get_field('field_5b303fe622a15')
? Maybe it’s a subfield ?
I’ve tested this code on a WP loop and it worked :
$count = (int) get_field('count');
if ( $count >= 0) {
$count++;
update_field('count', $count);
}
By the way, get_field and update_field can be used with the field’s name instead of the field’s key.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.