In our form, the user should enter his ID number, the ID should not be replicated it should be unique. I have found through my search that we have to use Hooks. But I don't know how to use hooks and where to place them.
Could you please assist me?
Hello and thank you for contacting the Toolset support.
Exactly, this will need custom code to be done. The custom code can be added in different places. Either in the theme's functions.php file or another file, in a separate plugin, or in Toolset->Settings->Custom Code. Read about the latest option here:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
I think that you are using a User registration form, right? In that case, take inspiration from this custom code https://toolset.com/forums/topic/phone-field-unique-in-the-user-registration/#post-1860755
If this is about a Post Form, check this one https://toolset.com/forums/topic/force-unique-field-on-cred-post/#post-621220
If you are unsure about it, allow me temporary access to your site and I'll see what I can do. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
It is difficult to give you access since our website should be accessed through a VPN.
I have added the below code in the toolset custom code snippet. but it doesn't work. What is the issue?
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
/**
* CRED custom validation to prevent duplicates CREATE FORM
*/
add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
$aid = $_POST['id-number1']; // Gets value from form
$unique_field = 'aid'; // Meta key stored in db
$form_ids = array( 7666 ); // 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' => $aid,
'meta_compare' => '=' ,
);
$matching = new WP_User_Query( $args );
$results = $matching->get_results();
if ( !empty( $results ) ) {
$errors[ $unique_field ] = $aid . ' is already in use.';
}
}
return array($fields,$errors);
};
I have changed the code,
the new one is below. But still not working
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
/**
* CRED custom validation to prevent duplicates CREATE FORM
*/
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
// if it is the specific CRED form
if (isset($form_data['id']) && $form_data['id']!=485516) return $error_fields;
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
$r=$fields['wpcf-id-number1'];
$query = new WP_Query( $r );
if( $query->have_posts() ) {
// Error "CPF already saved" is not working
$errors['wpcf-id-number1']='ID already saved';
}
return array($fields,$errors);
}
First, let's agree if this a User form, or a Post form. If it is a User Form, the second code is not correct. The first code may be correct, but it depends on what is the field slug. I am not sure if it is "id-number1" or "aid"?
Please note that WP_Query searches in the posts table and WP_User_Query searches in the users' table.
Let's say that this is about a User Form, that the number field was created by Toolset and its slug is "id-number1", and that the form ID is7666 , I believe the following code will work:
add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
$aid = $_POST['id-number1']; // Gets value from form
$unique_field = 'wpcf-id-number1'; // Toolset uses the prefix 'wpcf-' at the database level
$form_ids = array( 7666 ); // 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' => $aid,
'meta_compare' => '=' ,
);
$matching = new WP_User_Query( $args );
$results = $matching->get_results();
if ( !empty( $results ) ) {
$errors[ $unique_field ] = $aid . ' is already in use.';
}
}
return array($fields,$errors);
};
If it does not work for you, I'll need to take a closer look at your form, the custom fields, and the custom code that you are using. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Thank you for your support,
Sorry, I forget to mention that it is a post form, not a user form.
The field slug is (id-number1) and form Id is 7666
I cannot share the credentials because the website accessed using VPN. But I can share a backup from the duplicator plugin.
Because it is a Post from, we need to use WP_Query instead of WP_User_Query. The code becomes:
add_filter( 'cred_form_validate', 'uni_identifier' ,10,2);
function uni_identifier( $error_fields, $form_data) {
$aid = $_POST['id-number1']; // Gets value from form
$unique_field = 'wpcf-id-number1'; // Toolset uses the prefix 'wpcf-' at the database level
$form_ids = array( 7666 ); // 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' => $aid,
'meta_compare' => '=' ,
);
$matching = new WP_Query( $args );
$results = $matching->get_results();
if ( !empty( $results ) ) {
$errors[ $unique_field ] = $aid . ' is already in use.';
}
}
return array($fields,$errors);
};
I am setting your next reply as private to let you share a download link to the Duplicator backup, I'll build it locally and check it further. Please let me know what page or URL on the frontend where the form is being used. Please create an administrator user and share its credentials before taking the backup.
I built the Duplicator copy in my local setup, and I found one User form "OMREN services" it has several generic fields such as:
- ID Number
- Date of Birth
- Mobile Number
- Other Number
- Email
I assume that you are looking to make the ID Number unique, right?
If yes, this field should not be a generic field. It has to be a user field. Go to Toolset->Custom Fields, then User Fields(tab), and create a new Group with the field on it. hidden link
Then, go to the form and update it to include this field instead of a generic field. Then, then adapt the custom code to reflect the actual slug of the field that you will be creating. Does it make sense?
Dear Jamal,
thank you for your support,
yes, ID should be unique.
I have tried to add the newly created user field in the form but it comes like on the attached image and it does not appear when I run the form.
Would it be possible to access your live site or to work on a staging site? This way we will always be on the same version.
Otherwise, I can create a user field on my local copy and test it, but I won't be sure that we are having the same thing.
If you can't have a staging site, would you like to migrate your website to our platform? We'll work on it until we fix it then you can implement the same on your website.
I have got VPN access to the website. Could you please open a private reply to send you the access details
Of course. Your next reply will be private.
I installed the FortiClient software but I am not able to configure it. I guess that I need an IP address or something else.
hidden link
Can you walk me through the configuration?
please check the configuration hidden link
Even after login into the VPN, I am still rejected by the site. Check this screenshot hidden link
I suggest that you create only the custom field, and the form on one of our test sites, so I can help you with that, then you can adapt the solution to your website. Use the following link to login in hidden link