Custom validation on ajax request
-
Hello,
I would like to hook on ajax validation of the form and display some custom errors by some extra checks i am planning to do. Is there any function i could use to hook after form’s validation and display my messages and of course the success and error of the form.Thank you in advance!
-
Hi @zachostaf
I hope you are doing well.
You can use the forminator_custom_form_submit_errors:
$submit_errors = apply_filters( 'forminator_custom_form_submit_errors', $submit_errors, $form_id, $field_data_array );
A small example is found here:
https://gist.github.com/patrickfreitasdev/12e75dd199799532875b319871904bf1This verifies if email-1 & email-2 field match and return an error if not.
You can use the code as a mu-plugin:
https://premium.wpmudev.org/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsLet us know if you have any additional question.
Best Regards
Patrick FreitasThank you very much for the immediate reply!!
I am sorry for the delay but I tried also some other hooks I found for achieving my purposes. By this function I can control the fields’ errors but as I can see I cannot change the top general error message of the form “Error: Your form is not valid, please fix the errors!”. Is there any way to change it according to my needs?Best Ragards,
Tafralis ZachariasI also would like to ask If “forminator_custom_form_submit_response” hook can help in my situation, because I already tried to use it for redirection after successful form submission ($response[‘url’] = “link…”).
Finally, can I stop the form submission on “forminator_custom_form_before_save_entry” with a custom error?
Thank you very much for your help!
Best Regards
Hi there @zachostaf
You can change the submit error message right in the form UI itself. Click on the Submit button field in the form editor and, under the Labels tab there, enter the custom message you want to display.
https://monosnap.com/file/jxVdesAfByJSelDe5Tlems2oFy4GZxYou can also set the redirect after submission in the UI by going to the form’s Behavior settings, and selecting “Redirect user to a URL”.
As for stopping the submission with “forminator_custom_form_before_save_entry”, I’m not quite sure, so I’ll check with the developers to see what they’d suggest for that.
Cheers!
PatrickHi Patrick,
thanks for your answer. I already knew how to set these fields from plugin’s settings. I jsut wanted to ovveride them accordingly to some custom checks.As soon you have some programming answers about any of the above questions please let me know.
In order to be more specific about my intention I will share the flow I have in mind:
1) Before save entry, make some custom checks and return custom error messages (if is needed or let validation begins)
2)Make some extra custom checks on submit and return custom validation errors (already answered)
3)After succsfull save entry redirect to dynamic urlFor your kind convenience it would be be very helpfull for me If you can send me any realated documentation about all of these hooks.
Thanks for your valuable help till now!
Best Regards,
ZachariasHi @zachostaf
Thank you for response and additional explanation!
The error message is set via UI but there’s a filter for it too
return apply_filters( 'forminator_custom_form_invalid_form_message', $invalid_form_message, $form_id );
so I believe you could hook to it like e.g.
add_filter( 'forminator_custom_form_invalid_form_message', 'my_custom_error', 10, 2 ); function my_custom_error( $invalid_form_message, $form_id ) { if ( $form_id == "123" ) { $invalid_form_message = "my error"; } return $invalid_form_message; }
That’s just an example but I think it should give you the “foundation”.
As for changing redirect URL dynamically, take a look at these two code snippets that do it based on form data:
https://gist.github.com/wpmudev-sls/14435fac91e663b10694a2a445537a34
https://gist.github.com/wpmudev-sls/5af95872fe195ce45e50387ebfe03e91
Best regards,
AdamHello Adam,
Thanks for your reply! I already managed to return custom error message by using these hooks:
forminator_custom_form_submit_response
forminator_custom_form_ajax_submit_responseThe problem is that the submission is not stopping so the data are saved even I return the $respone array like below and the custom error message is appeared on the frontend. Is there any way to interrupt posted data to be saved via using the above hooks?
$response["success"]= false; $response["errors"]=array(); $response["message"]="Custom error message";
Otherwise, please advise me for better practice according to my previous answer on Patrick where I explained the programming flow.
Best Regards!
- This reply was modified 3 years, 9 months ago by zachostaf.
Hello there @zachostaf
Our devs provided an example using the forminator_custom_form_ajax_submit_response hook:
<?php add_filter( 'forminator_custom_form_ajax_submit_response', function( $invalid_form_message, $form_id ){ $some_condition = false; $submit_errors = array(); if ( ! $some_condition ) { $submit_errors[][ 'field_id' ] = __( 'Error message related to field' ); $response = array( 'message' => __( 'My custom error message' ), 'errors' => $submit_errors ); wp_send_json_error( $response ); } return $invalid_form_message; }, 20, 2 );
Hope that helps, let us know if further help is needed. ??
Thank you,
DimitrisHello @zachostaf ,
We haven’t heard from you for a while now, so it looks like you no longer need our assistance.
Please feel free to re-open this ticket if needed.
kind regards,
KasiaHey, sorry for capturing this thread. But the “forminator_custom_form_ajax_submit_response” filter is no more working. Did you people changed the filter name in one of the las updates?
Hi @michelyweb
Yes, our team is working to improve the hooks, can you change it to
forminator_form_ajax_submit_response
?Let us know the result you got.
Best Regards
Patrick FreitasThanks for the answer. So you changed API and not mentioned it in changelog (at least i can’t find that information)?
We’ve 9 client Sites with problems because of this. As a workaround, we had to create a custom shortcode for the success message instead of making use of the hook. This makes us more independent form code changes on your side. In the next project iteration we’ll remove Forminator completely from these projects because this is not the first time things like this happened.
Best
MarcoHi @michelyweb,
I understand your concern and I am extremely sorry about the inconvenience caused.
I have brought this to the notice of the Frominator team so that we could improve the workflow and try to avoid any such instances in the future.
Kind Regards,
Nebu JohnThank you @wpmudevsupport14
- The topic ‘Custom validation on ajax request’ is closed to new replies.