Startseite › Toolset Professional Support › [Gelöst] Forms force page reload before displaying validation error messages
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Karachi (GMT+05:00)
Verwandte Dokumentation:
Dieses Thema enthält 2 Antworten, hat 2 Stimmen.
Zuletzt aktualisiert von alexG-4 vor 5 Jahren, 9 Monaten.
Assistiert von: Waqar.
I have what seems like a simple requirement, but I can see no way to fulfill it..
I have a page displaying posts of a custom post type, and a form for adding new posts. The form is hidden until the user clicks a "New" button.
When the user attempts to add a new post, I want the page to reload (and thus show the new post) if there are NO validation errors, but I DON'T want it reloaded if there ARE validation errors.
This works fine for validation messages arising from a required field (as set in the custom field definition) not being completed. The message just appears on the form.
But, it there are validation errors because of checks made using the "cred_form_validate" hook, then the page DOES get re-loaded before the validation error messages get displayed. And it's that which is causing me a real headache! The page reload hides the form so the validation messages aren't visible.
I've tried setting "Submit this form without reloading the page (use AJAX)" to ON. That lets me see the validation errors, but obviously it doesn't reload the page on success. So I tried forcing a reload within the "cred_submit_complete" hook, and even within the WP "save_post" hook. But in both cases I get a popup saying "There was an error while submitting the form".
It's a sort of catch 22.
I'm wondering whether I'm missing something obvious.
Hi Alex,
Thank you for contacting us and I'll be happy to assist.
Your observation is correct and the way the "cred_form_validate" hook works, page reload can't be avoided for validation through it.
From your message, it seems that you're using some script to hide the form, on page reload and then it shows when a button is clicked.
You can try an alternate approach. You'll note that when the form is submitted but there are errors registered through the "cred_form_validate" hook, "_tt" URL parameter is appended to the URL, e.g.
page-URL/?_tt=1552915291
But when the form is submitted successfully without any errors, additional URL parameters are appended:
page-URL/?_tt=1552915291&_success_message=817_1&_target=844#cred_form_817_1
You can use these parameters to detect the current state of the page and show/hide content conditionally using a custom shortcode and the conditional blocks:
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ )
For example:
add_shortcode('show_form_content', 'show_form_content_func'); function show_form_content_func() { if( (!isset($_GET['_tt'])) || (!empty($_GET['_success_message'])) ) { return 'true'; } else { return 'false'; } }
The above shortcode will return 'true' when the page has reloaded for the first time or if the form has been submitted successfully and 'false' when the form has been submitted, but there are validation errors.
Note: Please remember to register your custom shortcode into "Third-party shortcode arguments" section as explained at:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes
You can then set up your content and form on the page like this:
[wpv-conditional if="( '[show_form_content]' eq 'false' )"] // add form's shortcode here [/wpv-conditional] [wpv-conditional if="( '[show_form_content]' eq 'true' )"] // add regular content and the add new link to show the form <a href="page-URL/?_tt=show_form">Add new</a> [/wpv-conditional]
As a result, when the page will reload, initially only the content and the add new link will show.
( note: replace 'page-URL' with the actual link to the current page)
When the add new link will be clicked the page will reload, but this time only the form will show.
And after that, the form will automatically hide and the regular content will start showing, only when the form will submit successfully without any errors.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Thanks for these ideas, Waqar.
They helped me try various approaches, but nothing properly resolved the problem in a satisfactory way.
I'm going to have to get some personalized help on this later on in my project development.
Alex