Skip Navigation

[Resolved] Avoid duplicate value on custom field in User Forms Submmission

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by darmaJ-2 5 years ago.

Assisted by: Shane.

Author
Posts
#1366593

Hi,

I was looking something like this :
https://toolset.com/forums/topic/is-it-possible-to-avoid-duplicate-value-submission-for-any-cpt-field/

But, instead in CPT, I want it in User Forms that I created, do you can help me achieve it?
The idea is I want to avoid the same value for phone number when user register in my site, so when there is same value in that field there will be a error message when user submit the form

This is the code from above link for your reference :

/**
 * CRED custom validation to prevent duplicates
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
 
    $post_type = 'cpt-slug';  // Edit
    $unique_field = 'wpcf-' . 'field-slug';  // Edit
    $form_ids = array( 101, 115 );  // Edit IDs of CRED forms
 
    list( $fields,$errors ) = $error_fields;
 
    if ( in_array($form_data['id'], $form_ids ) ) {
 
        // Get existing posts with unique field
        $args = array(
            'post_type'     =>   $post_type,
            'post_status'   =>   'publish',
            'numberposts'   =>   -1,
            'meta_key'      =>   $unique_field,
            'meta_value'    =>   $fields[ $unique_field ]
        );
 
        $matching = get_posts( $args );
 
        if ( !empty( $matching ) ) {
 
            $errors[ $unique_field ] = 'Value already used';
        }
    }
 
    return array($fields,$errors);

Thanks

#1366839

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Darma,

Thank you for getting in touch.

Is it that the code is not working for you ? Or you have not tried it as yet.

If so then you need to use the code below for users.



/**
 * CRED custom validation to prevent duplicates
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {
  
  
    $unique_field = 'wpcf-' . 'field-slug';  // Edit
    $form_ids = array( 101, 115 );  // Edit IDs of CRED forms
  
    list( $fields,$errors ) = $error_fields;
  
    if ( in_array($form_data['id'], $form_ids ) ) {
  
        // Get existing posts with unique field
        $args = array(
    'meta_key'     =>  $unique_field,
    'meta_value'   => $fields[ $unique_field ],
    'meta_compare' => '=',
);
        $matching = get_users( $args );
  
        if ( !empty( $matching ) ) {
  
            $errors[ $unique_field ] = 'Value already used';
        }
    }
  
    return array($fields,$errors);

Replace the 'field-slug' with the slug of your field as well as remove 101 and 115 and add the ID of your form here.

Please try this and let me know if this helps.
Thanks,
Shane

#1367041
Screen Shot 2019-10-23 at 11.10.20.png

Hi Shane,

Thanks for your prompt reply, but after I implement the code above into my function.php, it resulted to be something like in the attached image.

The code that I paste in the function.php is something like this :

/**
 * CRED custom validation to prevent duplicates
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {


    $unique_field = 'wpcf-' . 'nomor-telepon-user';  // Edit
    $form_ids = array( 67, 117 );  // Edit IDs of CRED forms

    list( $fields,$errors ) = $error_fields;

    if ( in_array($form_data['id'], $form_ids ) ) {

        // Get existing posts with unique field
        $args = array(
    'meta_key'     =>  $unique_field,
    'meta_value'   => $fields[ $unique_field ],
    'meta_compare' => '=',
);
        $matching = get_users( $args );

        if ( !empty( $matching ) ) {

            $errors[ $unique_field ] = 'Value already used';
        }
    }

    return array($fields,$errors);
  }

"nomor-telepon-user" is the text field that I want to avoid duplication and "67" & "117" is Form ID

#1367653

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Darmaj,

I see the issue, change this line here from :

add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );

To

add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' ,10,2);

This should resolve the issue.

Thanks,
Shane

#1368337

Hi Shane,

thanks for your reply, yes it already resolve the submission issue, but when I test it still can register the user that have the same phone number, do you know what went wrong?

#1369399

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Darmaj,

Would you mind providing me with admin access to the site so that I can see exactly why this isn't working for you ?

The private fields will be enabled for your next response.

Also please let me know the page where the form was added.

Thanks,
Shane

#1370199

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Darmaj,

I've fixed it for the new user form.

So if you try to create a new user with an already existing number the error will be thrown.

Thanks,
Shane

#1371217

Hi Shane,

Thanks it already worked as I expected!

#1371219

My issue is resolved now. Thank you!