Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active November 6, 2022 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
@Alhuz98
Copy link

Alhuz98 commented Aug 23, 2021

Hi, @patrickfreitasdev, I am using forminator in my website and want to add customize filter to prevent redundancy, however the
$submit_errors[][ 'email-2'] = __( 'Emails does not match' ); is not working

the message is Error: Your form is not valid, please fix the errors!

@patrickfreitasdev
Copy link
Author

Hi, @patrickfreitasdev, I am using forminator in my website and want to add customize filter to prevent redundancy, however the
$submit_errors[][ 'email-2'] = __( 'Emails does not match' ); is not working

the message is Error: Your form is not valid, please fix the errors!

Hi there, The form will show "Your form is not valid, please fix the errors!" but this should output below to email two what you define on

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

Feel free to open a thread on Forminator wp.org and we can take a closer look :)

@Alhuz98
Copy link

Alhuz98 commented Aug 24, 2021

Alright thank you, May I know which directory is suitable to put this file in wordpress?

@patrickfreitasdev
Copy link
Author

@Alhuz98
Copy link

Alhuz98 commented Aug 25, 2021

Great, Thank you

@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