How to Make a Forminator Field Unique (Prevent Duplicate Submissions)
-
Hello,
I’m using Forminator Forms on my WordPress website, and I want to make a specific field (e.g., email field) unique, meaning users should not be able to submit the form if the same email has already been used.
I tried using custom code to achieve this, but I encountered an issue where instead of showing my custom error message, Forminator displays the generic “Error: Your form is not valid, please fix the errors!”
Here’s the code I used:
add_filter( 'forminator_custom_form_submit_errors', 'check_form_duplicate_submission_data', 99, 3 );
function check_form_duplicate_submission_data( $submit_errors, $form_id, $field_data_array ) {
$form_ids = array( 23370 ); // Form IDs
$fields_ids = array( 'email-1' ); // Email field name
$err_msg = 'This email has already been used. Please use another email!'; // Custom error message
if ( !in_array( $form_id, $form_ids ) ) {
return $submit_errors; // Skip checks if form ID does not match
}
foreach( $field_data_array as $key => $value ) {
$field_name = $value['name'];
if ( in_array( $field_name, $fields_ids ) ) {
$entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( $form_id, $field_name, $value['value'], '*' );
if ( $entries ) {
$submit_errors[$field_name] = [$err_msg]; // Assign error to the field correctly
// ?? Force remove the generic "Your form is not valid" message
if ( isset($submit_errors['submit']) ) {
unset($submit_errors['submit']);
}
}
}
}
return $submit_errors;
}Issues I’m Facing:
- The code does detect duplicate emails.
- It correctly displays my custom error message.
- However, the generic Forminator error message still appears along with my custom message.
Is there a way to properly remove the generic error message and only show my custom one?
Would appreciate any insights or alternative solutions!
Thanks in advance. ??
- You must be logged in to reply to this topic.