Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active November 6, 2022 13:41
Show Gist options
  • Save patrickfreitasdev/12e75dd199799532875b319871904bf1 to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/12e75dd199799532875b319871904bf1 to your computer and use it in GitHub Desktop.
<?php
add_filter('forminator_custom_form_submit_errors', 'my_custom_form_validation', 10, 3);
function my_custom_form_validation($submit_errors, $form_id, $field_data_array) {
// Skip if is not the correct form
if( (int) $form_id !== 49 ){
return;
}
$fields = [];
$fields[] = $_POST['email-1'];
$fields[] = $_POST['email-2'];
// Check if both emails are the same
if (count(array_unique($fields)) !== 1) {
// Custom error
$submit_errors[][ 'email-2'] = __( 'Emails does not match' );
}
// Always return $submit_errors
return $submit_errors;
}
@PaulVleeshouwers
Copy link

Thank you for this script.
How can I add multiple form id numbers?

@patrickfreitasdev
Copy link
Author

Thank you for this script. How can I add multiple form id numbers?

You can use something like this:

$forms = [1,2,3,4];

// Skip if is not the correct form
if( !in_array( $form_id, $forms ){
return;
}

Reference https://www.php.net/manual/en/function.in-array.php

@inspiredearth
Copy link

@patrickfreitasdev Hi Patrick,
I am attempting to use this code you've kindly provided. It works well, except for one thing. The message it displays when the two email addresses don't match is: "Error: Your form is not valid, please fix the errors!"

That's not what I'd like to display, as it's not useful. Is there a way to change this?

@patrickfreitasdev
Copy link
Author

Hi @inspiredearth I hope you are doing well.

It is the default behaviour on Forminator, on top of Form it shows the "Error:... " and on the field level it will show what you define at:

$submit_errors[][ 'email-2'] = __( 'Emails does not match' );

However, feel free to open a ticket on wordpress.org > Forminator > Support and we can take a look if this can be changed.

@inspiredearth
Copy link

Hi @patrickfreitasdev , Thanks for that info.
It seems to be that the current wording isn't ideal. It makes it sound like there is an error with the form itself.
I suspect it would be clearer and less ambiguous if it stated something like, "Submitted form data is invalid. Check for error messages within the form". Something along those lines.

I'll post a support ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment