Home › Forums › Bug Reports › 5.7 – AJAX validation broken on acf_form() front-end › Reply To: 5.7 – AJAX validation broken on acf_form() front-end
I’m running into the same problem with Version 5.8.1 (I keep being sent to the WordPress error page when I hit submit on a form that is loaded through AJAX, if some of the required fields are not filled out).
I found part of a solution. AJAX validation does work for required fields if I add
acf_form() somewhere on the page itself.
You can’t only have
acf_form() in an AJAX action. It has to be on the page also. Otherwise it seems
acf_form_head() would just assume there is no
acf_form() on the page at all. I guess that’s why AJAX validation didn’t work.
I don’t actually want there to be two forms on the page, so I just put
acf_form() in the page template like this:
<?php acf_form(array('form' => false,)); ?>
So that this extra
acf_form() is invisible on the page, and the
acf_form() in my AJAX action is the only one that actually submits any data.
Now another problem: on submit, I’m redirected to
(my-site-url)/wp-admin/admin-ajax.php?updated=true, instead of staying at the same url. The data submitted is still saved, though.
'return' => '(url-to-redirect-to)', to my AJAX
acf_form() does work, so at least there is a way to cobble together something that looks okay. All I have to do is add a redirect to a thank you page.
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!
© 2022 Advanced Custom Fields.