Hi!
Is it possible to make the phone field unique in the user registration?
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
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);
};
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,
My issue is resolved now. Thank you!
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'] );
}
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?
Yes, it's the other ticket I want to solved. This one is solved.
Thank you
Resolving this ticket per client instruction.