• Resolved Agencia 221B

    (@agencia221b)


    Hi guys!

    I’m meaning to ask you guys about this issue for a lng time and always forget:

    Some time ago I needed to restrict form submissions on duplication of text field (CPF, which is a social security number equivalent here in Brazil).
    You guys gave me this snippet, that works flawlessly:

    <?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,32319,33093,33423,33523,33588,33636,33691,33741,33764,33777,33835,33866,33879,33881,33908,33936];
    	if( empty( $submit_errors ) && in_array( $form_id, $your_list_forms ) ){
    		$your_unique_field_name = 'text-1';
    		$your_error_msg = 'Já existe uma inscri??o com esse CPF.';
    		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 = 'text-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);

    My problem is: as you can see, I have now a very long stream of form ID’s in what this code must act on, and this code act only in those forms AND in the field with the name ‘text-1’.

    What I need: that this code doesn’t need the forms ID’s to work on the form AND it doesn’t target the field’s name, but instead the field’s class. That way I don’t have to edit the snippet every new form, and some times I delete the first text field that I create and then I have to restar the form creation by scratch because the next text fields are named sequencially from 2 and forth.

    Is possible to edit this code do exaclty the same function as does now, but without the need to insert the form’s ID AND targeting a specific custom class atached to any text field instead of the field’s name?

    Thanks in advance, and again: huge fan, u guys rock!

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.

    I have asked our developers if the code could be further changed to work based on the field’s class name instead of the field ID. We’ll update you here once we have more feedback on this as soon as possible.

    Kind Regards,
    Nebu John

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @agencia221b

    This snippet should help:

    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ){
    	if ( empty( $submit_errors ) ) {
    		$your_unique_field_name = array( 'text-1', 'text-2', 'text-3', 'text-4' );
    		$your_error_msg = 'Já existe uma inscri??o com esse CPF.';
    		foreach ( $field_data_array as $field ) {
    			if ( in_array( $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 = %s AND m.meta_value=%s AND e.form_id = %d LIMIT 1;", $field['name'], $field['value'], $form_id ) ) ){
    					$submit_errors[][$your_unique_field_name] = $your_error_msg;
    				}
    				break;
    			}
    		}
    	}
    	return $submit_errors;
    }, 10, 3);

    Kind Regards,
    Kris

    Thread Starter Agencia 221B

    (@agencia221b)

    Hey, Kris. Thank you so much for your answer!

    Maybe i’m wrong, but I understand that this code that you provided target fields with specific names (text-1, text-2 and so on). But what I need is to check duplicate submission in a field with a specific class, lets say ‘.no-double’

    So in any form that I have any field with that class, the code will work on preventing the submission if that field with .no-double class has a duplicate value already submited.

    thanks again!

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @agencia221b

    I pinged our SLS Team to review this query. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @agencia221b,

    Could you please check and see whether the following snippet helps?

    <?php
    
    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ){
    	if ( empty( $submit_errors ) ) {
    		$your_error_msg = 'Já existe uma inscri??o com esse CPF.';
    		foreach ( $field_data_array as $field ) {
    			if ( ! empty( $field['field_array']['custom-class'] ) ) {
                    if ( false !== strpos( $field['field_array']['custom-class'], 'no-double' ) ) {
                        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 = %s AND m.meta_value=%s AND e.form_id = %d LIMIT 1;", $field['name'], $field['value'], $form_id ) ) ){
                            $submit_errors[][$field['name']] = $your_error_msg;
                        }
                        break;
                    }
    			}
    		}
    	}
    	return $submit_errors;
    }, 10, 3);

    The following line in the above snippet defines the custom class ie: no-double:
    if ( false !== strpos( $field['field_array']['custom-class'], 'no-double' ) ) {

    Please do change it according to your requirements.

    You can implement the above snippet using mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Best Regards,

    Nithin

    Thread Starter Agencia 221B

    (@agencia221b)

    I don’t have words! This is why Forminator is by far the best form plugin for wordpress.

    The snippet works flawlessly. Perfect, exactly what I need.

    Thank you so much!

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