Skip Navigation

[Resolved] phone field unique in the user registration

This support ticket is created 3 years, 4 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by Christian Cox 3 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1860113

Hi!
Is it possible to make the phone field unique in the user registration?

#1860699

Hello, there is nothing built-in to Toolset that will enforce unique custom field values, but another ticket discusses an approach that uses the Forms API cred_form_validate to validate uniqueness of a User custom field by querying Users with that field value to determine if a specific meta value already exists in the database: https://toolset.com/forums/topic/unique-user-field-value-form-to-prevent-duplicate-values/

This example uses an email address, which is fairly straightforward to compare. Phone numbers may be more complex to validate, however, since no format is enforced during data entry. There could be spaces, parentheses, dashes, periods, etc. to consider.

Documentation for this validation API is available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#1860755

Let's understand that everyone puts the number followed and in the same way. I have tried this code but it doesn't work.
Where can the error be?

/**
 * CRED custom validation to prevent duplicates EDIT FORM - excludes current user from the query as it will find a matching nickname
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' ,10,2);
function prevent_duplicate_submissions( $error_fields, $form_data) {
    $current_user = get_current_user_id();
    $nickname = $_POST['telefono_movil']; // Gets value from form
    $unique_field = 'telefono_movil';  // Meta key stored in db
     
    $form_ids = array(  6729 );  // 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' => $telefono_movil,
            'meta_compare' => '=' ,
            'exclude' => $current_user
            );
     
        $matching = new WP_User_Query( $args );
         
        $results = $matching->get_results();
        if ( !empty( $results ) ) {
            $errors[ $unique_field ] =  $telefono_movil . ' is already in use. Choose a different phone.';
        }
    }
  
    return array($fields,$errors);
};
#1860839

Here are some potential problems:

1. Types field slugs in the database use a wpcf- prefix, so if you created these User meta fields in Types you should add the wpcf- prefixes:

    $nickname = $_POST['wpcf-telefono_movil']; // Gets value from form
    $unique_field = 'wpcf-telefono_movil';  // Meta key stored in db

2. The variable $telefono_movil is undefined:

'meta_value' => $telefono_movil,
#1860863

My issue is resolved now. Thank you!

#1860945

Sure,

It should be like that.
But I don't know what you mean by this
The variable $telefono_movil is undefined: 'meta_value' => $telefono_movil,


//AÑADIR MOVIL AL API USER
    /**
     * Adds user_meta to rest api 'user' endpoint.
     */
    function adding_telefono_movil_rest() {
        register_rest_field( 'user',
            'telefono_movil',
            array(
                'get_callback'      => 'telefono_movil_callback',
                'update_callback'   => null,
                'schema'            => null,
            )
        );
    }
    /**
     * Return user meta object.
     *
     * @param array $user User.
     * @param string $field_name Registered custom field name ( In this case 'user_meta' )
     * @param object $request Request object.
     *
     * @return mixed
     */
    function user_meta_callback( $user, $field_name, $request) {
        return get_telefono_movil( $user['id'] );
    }
#1861443
Screen Shot 2020-11-30 at 7.07.10 AM.png

I mean the variable $telefono_mobile was undefined in your phone number validation code. The variable had no value in your previous code, so it served no purpose. See the attachment from your previous comment.

Your recent reply includes some other code, not related to validation. It seems to be related to adding information in the REST API, maybe for another ticket?

#1861587

Yes, it's the other ticket I want to solved. This one is solved.

Thank you

#1861699

Resolving this ticket per client instruction.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.