Second validation message with rwmb_frontend_after_display_confirmation
Support › MB Frontend Submission › Second validation message with rwmb_frontend_after_display_confirmation
- This topic has 11 replies, 3 voices, and was last updated 2 years, 4 months ago by
Graceson.
-
AuthorPosts
-
September 23, 2022 at 6:18 AM #38397
Graceson
ParticipantHello,
I am trying to add a second validation message to my front-end form. The reason is that I am building a contact form that allows people to register for a newsletter. I'm happy with the default validation for handling the form data as it is saved into the custom table I have set up.
Then, I am calling rwmb_frontend_after_process to add some custom data to the form after is has been submitted. This is all working fine. However, within rwmb_frontend_after_process, I would like to have a conditional that allows me to display an additional message using rwmb_frontend_after_display_confirmation. Here is a sudo example of what I am trying:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { insertInMailchimpList(); if (insertInMailchimpList() == "failed") { add_action( 'rwmb_frontend_after_display_confirmation', function( $config ) { echo "<div class=\"rwmb-error\">Unfortunately, your newsletter subscription failed.</div>"; }, 9, 1 ); } $data = [ 'field_1' => 'value 1', 'field_2' => 'value 2', 'field 3' => ['one', 'two', 'three'], ]; \MetaBox\CustomTable\API::update( $post_id, 'my-custom-table', $data ); }, 10, 2 );
Unfortunately, it appears that using
add_action( 'rwmb_frontend_after_display_confirmation', ...
insideadd_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {}
does not work at all.Is there a work around, or is this simply not possible?
Thanks,
GracesonSeptember 23, 2022 at 7:23 AM #38398Graceson
ParticipantI also thought that I'd add that I tried some other options as tests. My hunch is that
rwmb_frontend_after_display_confirmation
runs beforerwmb_frontend_after_process
and that is why nesting the hooks as I did above does not work.However, I tried other options that also do not work. For example this code does not output another message as desired:
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) { add_action( 'rwmb_frontend_after_display_confirmation', function( $config ) { echo "<div class=\"rwmb-error\">Unfortunately, your newsletter subscription failed.</div>"; }, 9, 1 ); return $validate; }, 10, 2 );
I'm kind of at a loss of what else to try. I have yet to figure out any way to dynamically add a message depending on what the user submitted.
September 27, 2022 at 6:13 AM #38431Long Nguyen
ModeratorHi,
Instead of adding the HTML code to a second hook, you can concatenate it to the validation message. For example:
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) { if ( empty( $_POST['text_rt5pl53zoi'] ) ) { $validate = 'Please add a text'; $validate .= "<div class=\"rwmb-error\">Unfortunately, your newsletter subscription failed.</div>"; } return $validate; }, 10, 2 );
Read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#validation
September 27, 2022 at 6:32 AM #38432Graceson
ParticipantHi Long,
Thank you for this additional idea. Is there a way to add the message without throwing the validation error?
If I use this example, my code will likely end up looking something like this (untested sudo-code):
add_filter( 'rwmb_frontend_validate', function( $validate, $config ) { if ( !empty( $_POST['subscribe_to_newsletter'] ) ) { add_email_to_Mailchimp(); if ( add_email_to_Mailchimp() == 'error') { $validate .= "<div class=\"rwmb-error\">Unfortunately, your newsletter subscription failed. </div>"; } } return $validate; }, 10, 2 );
However, this means that validation for the form submission will depend on the Mailchimp API response as handled by my
add_email_to_Mailchimp();
function. I do not want that behavior.I want the form to be able to submit, but to also display an additional message to tell the user whether or not they were successfully subscribed after I have a chance to process all the data and transmit information to Mailchimp.
September 28, 2022 at 3:32 PM #38452Long Nguyen
ModeratorHi,
There is no form hook or post hook that supports this case. I think you can use the hook
rwmb_frontend_after_process
and create some JS code to append the HTML code to the form after processing.
https://docs.metabox.io/extensions/mb-frontend-submission/#form-hooksGood luck.
October 19, 2022 at 12:46 AM #38743Graceson
ParticipantHi Long,
That makes sense. I am working on that solution now; however I am having trouble with that hook still.
I have written some JS code to display my message, however, that code doesn't seem to run properly when called from
rwmb_frontend_after_process
.To keep things simple, it looks like I can't even log something to the console. For example the following does not work!
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { echo '<script> console.log("rwmb_frontend_after_process can run JS"); </script>'; //WILL NOT LOG TO CONSOLE $data = [ 'field_1' => 'value 1', 'field_2' => 'value 2', 'field 3' => ['one', 'two', 'three'], ]; \MetaBox\CustomTable\API::update( $post_id, 'my-custom-table', $data ); }, 10, 2 );
Now, I know this code inserts the script properly because I can see the console log if I add
die;
right after the echo used to insert the script.Trying to make the JS run after the DOM is loaded also doesn't work
echo '<script> window.addEventListener(\'load\', function() { console.log("rwmb_frontend_after_process can run JS"); }) </script>'; //DOES NOT WORK
Is there a different hook I need to use, or is there some modification to the JS that will make it work?
October 19, 2022 at 9:44 PM #38748Long Nguyen
ModeratorHi,
The action is run behind the scene, the page is reloaded after clicking the submit button so if you use the JS code to print out something, it will not display. You should use it to trigger an action like updating something to the database or sending an email to an address to see how it works.
October 20, 2022 at 12:05 AM #38754Graceson
ParticipantOkay, I understand that the page reloads, and so I can not use JS to print out something. However, I am trying to modify the HTML content of the page to display a message using JS per your suggestion. This is what you suggested:
I think you can use the hook
rwmb_frontend_after_process
and create some JS code to append the HTML code to the form after processing.So if the page reloads, how am I supposed to append the HTML? I could write something to the database and then recall it after the reload, but that seems excessive for a task like this. I'm already using php to send the emails that I want. So, I don't understand how I am supposed to use
rwmb_frontend_after_process
to run some JS that will alter the HTML of the page.Now, my JS function works perfectly inside
rwmb_frontend_before_display_confirmation
; however, it appears that I can not userwmb_frontend_before_display_confirmation
insiderwmb_frontend_after_process
.For reference, here is the JS function that I wrote to display the message as desired:
function subscriptionMessage(status = "none") { //Wait until the page exists. window.addEventListener('load', function() { console.log("subscription validation running"); const mbValidationMessage = document.querySelector(".rwmb-confirmation"); const message = document.createElement("div"); if (status == "success") { message.className = "rwmb-confirmation"; message.innerHTML = "Thanks for subscribing to our newsletter!"; } else if (status == "error") { message.className = "rwmb-error"; message.innerHTML = "Uh oh, we could not subscribe you to our newsletter."; } else { return; } mbValidationMessage.parentNode.insertBefore(message, mbValidationMessage.nextSibling); }) }
October 20, 2022 at 10:17 PM #38768Long Nguyen
ModeratorHi,
Thanks for your suggestion. I've tried to use the hook
rwmb_frontend_after_process
to add some HTML code to the form but no luck. It does not look to work. The problem is the action hookrwmb_frontend_before_display_confirmation
does not support accessing the value via the$_POST
variable to create a validation. But I see it will call the templateconfirmation.php
to display the confirmation text.Another way is to add the JS code directly to the template, I've tested and it worked on my end, screenshot https://monosnap.com/file/Ugk04bNCk1itqGquUQYDpPE14eew9q
You can follow this documentation to know how to override the template of this extension on your site https://docs.metabox.io/extensions/mb-frontend-submission/#post-template-files
Let me know if it helped.
December 15, 2022 at 7:16 AM #39809Graceson
ParticipantHi Long,
I thought about using the template. However, I am making a plugin so I didn't want to use my child theme.
Ultimately, I decided to have my PHP functions set a session cookie to indicate success or failure. When my script runs, it uses the value of that cookie to display the proper message, and then destroys the cookie.
I tried putting the script in
rwmb_frontend_after_display_confirmation
, however that hook is broken when using AJAX submit on the form. Therefore, I ultimately had to put my script directly in the text of the normal MetaBox confirmation message.Do you know if that AJAX bug has been fixed yet? I haven't had a chance to test yet, but the changelog for AIO 1.16.7 doesn't mention anything.
December 17, 2022 at 5:41 PM #39889Peter
ModeratorHello,
That Ajax issue has been fixed a few days ago and will be included in the next release of MB Frontend Submission. Sorry for the slow update because we have a list of issues with a higher priority to do.
January 4, 2023 at 1:22 AM #40110Graceson
ParticipantHi Peter,
That is good to hear. Thanks for the update. Do you know when this new version of MB Frontend Submission will be released? Looks like it is not in AIO 1.16.9.
Thanks,
Graceson -
AuthorPosts
- You must be logged in to reply to this topic.