Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @agencia221b,

    I hope you are keeping well and thank you for reaching out to us.

    Yes, you can use the same code to prevent duplicate entries in a text field. Simply replace the form ID and the name of the field in the code provided above.

    The form ID should be replaced in the following line (line no. 16):
    $your_list_forms = [123,456];

    and the name attribute of the field in the following line:
    $your_unique_field_name = 'email-1';

    Please refer to the following screenshot to identify the name of the field: https://ibb.co/4NwpzSz

    I hope that helps, please feel free to get back to us if you need any further assistance.

    Kind Regards,
    Nebu John

    Thread Starter Agencia 221B

    (@agencia221b)

    Hi Nebu, thanks for your reply.

    Unfortunately it doesn,t seem to be working.

    I changed the form ID and the unique_field_name but it just allows duplicate values =/

    I have a script that validates social security number (in my case CPF) in a text field, that is the one that I can’t have duplicate submissions, but as soon as I change the unique field name to samething other than email-1 it stops working. And I tested in other text field without this validation script, and just doesn’t work either.

    If you wanna take a look in my page to see if there is something strange, in this page https://www.enfpt.org.br/teste/ I have a test form, with two text fields (and a name field). The first one is the CPF that I need the code to work on, the second one is just a simple text field. Right now the code is set to target text-2 (the second text field in this test form) but just doesn’t.

    Very appreciate if you can suggest some other tip.

    Thanks in advance.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @agencia221b

    Thanks for response!

    Just to make sure, can you share the code (the one for duplication prevention) back with us after the changes you made? I mean – exactly as it is on your site now. I just want to make sure it’s correctly configured.

    More importantly though: the CPF field is the one for social security so this is also the one that’s targeted by the other script – the validation one – right?

    What happens if you temporarily remove that other (social security) validation script (despite it being hooked to a different field) and only keep the one for checking duplicates – does it start to work or is it still not working?

    Best regards,
    Adam

    Thread Starter Agencia 221B

    (@agencia221b)

    Hi Adam!

    Thanks for your reply.

    Sure, here it is the code that i’m using right now on that test page:

    <?php
    /**
     * Plugin Name: [Forminator] - Limit the submition by email.
     * Description: [Forminator] - Limit the submition by email.
     * Author: Thobk @ WPMUDEV
     * Author URI: https://premium.wpmudev.org
     * License: GPLv2 or later
     */
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    } elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ){
    	$your_list_forms = [28080,28152,29432,29863,30051,31881];
    	if( empty( $submit_errors ) && in_array( $form_id, $your_list_forms ) ){
    		$your_unique_field_name = 'text-2';
    		$your_error_msg = 'Já existe uma inscri??o com esse email.';
    		foreach( $field_data_array as $field ){
    			if( $field['name'] === $your_unique_field_name ){
    				global $wpdb;
    				$table_meta = $wpdb->prefix . 'frmt_form_entry_meta';
    				$table_entry = $wpdb->prefix . 'frmt_form_entry';
    				if( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(1) FROM $table_meta as m LEFT JOIN $table_entry as e ON m.entry_id = e.entry_id WHERE m.meta_key = 'email-1' AND m.meta_value=%s AND e.form_id = %d LIMIT 1;", $field['value'], $form_id ) ) ){
    					$submit_errors[][$your_unique_field_name] = $your_error_msg;
    				}
    				break;
    			}
    		}
    	}
    	return $submit_errors;
    }, 10, 3);

    Just an observation: right now is set to ‘text-2’, because is easier for us to test the feature without the need of a valid CPF. But I have tested with this code edited to ‘text-1’ that is the CPF field.

    Answering your other questions:

    More importantly though: the CPF field is the one for social security so this is also the one that’s targeted by the other script – the validation one – right?

    Yes, that’s correct. Here it is the script that makes the CPF validation, if you want to take at look: https://drive.google.com/file/d/1Ze-iRWtmtr5sQ6Vp9D4kcbWhCzWUGqVA/view?usp=sharing

    What happens if you temporarily remove that other (social security) validation script (despite it being hooked to a different field) and only keep the one for checking duplicates – does it start to work or is it still not working?

    I have tried, and the bug continues. Matter of fact, I’ve tried in other wordpress instalation, a fresh one, withou the CPF validation script, and still did’t work.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @agencia221b

    I hope you are doing well.

    It is happening because the Select is looking specifically for an email, in the Select query you can replace the:

    WHERE m.meta_key = 'email-1'

    To the field, you are looking to compare, in your case text-2

    WHERE m.meta_key = 'text-2'

    https://monosnap.com/file/QH7lpxGJXYpbcOGxUdMnZ0yfzXYFTF

    Best Regards
    Patrick Freitas

    Thread Starter Agencia 221B

    (@agencia221b)

    Yes, Patrick!

    The code in fact have a second mention to the field that Forminator shouldn’t have a duplicate. I change this second field too and now it works flowlessly.

    Matter of fact, now I have two instances of this code to check for duplicates in both email and CPF, and is working like charm.

    Thank you so much for the help, you guys rock.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Restrict form submissions on duplication of text field’ is closed to new replies.