Skip Navigation

[Resolved] Making the Email field in a form unique

This support ticket is created 2 years, 6 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by Minesh 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2173755

We have a custom post type resembling a membership set-up.
When a member registers a profile we need the system to verify the email address is unique in the CPT. We do have a separate email field in the CPT.

The current setup is as follows:
user registers as a WordPress user. After this, they then complete it as part of their profile cred form. the cred form has two fields of interest. The one is membership type and the other is directory entry.
There is also an email field in the CPT. We need to enforce the single use of an email address in the profile. The email field must verify that the same email is not used. We need this check done on the first entry of the email as well as a change of email address.

There are two different forms the one is an add email and the one is an edit form.

Here is the function code that t is not working. any advice would be appreciated


add_filter('cred_form_validate','func_validate_email',10,2);
function func_validate_email($error_fields, $form_data){
  
    //field data are field values and errors
  
    list($fields,$errors)=$error_fields;
  
     if ($form_data['id']==1440){
          
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-email',
                              'value' => $_POST['wpcf-email']
                            )),
                    'post_type' => 'custom-post-type',
                    'posts_per_page' => -1
                    );
            $posts = get_posts($args);
  
  
        //check if email value is already on the database
  
        if (count($posts) > 0){
  
            //set error message for my_field
  
            $errors['wpcf-email']='This E-mail already registered.';
        } 
    } 
    return array($fields,$errors);
}


The HTML code of the form where we this done is below:

[credform]

<!--begin page container formating-->	

  <div class="container">
  
<!--begin errors formating-->	
  
    <div class="errors">
		[cred_field field='form_messages' class='alert alert-warning']
	</div>
	
<!--end errors formating-->	

<!--begin contentcontainer formating-->
    
  	<div class="contentcontainer">
    
          <div class="form-group llh">
              <label for="%%FORM_ID%%_landline-nr-h">
                	[cred_i18n name='landline-nr-h-label']
                		Landline Nr (H)
                	[/cred_i18n]
              </label>
              		[cred_field field='landline-nr-h' placeholder='Landline Home' force_type='field' class='form-control' output='bootstrap']
          </div>
  
          <div class="form-group llw">
              	
              <label for="%%FORM_ID%%_landline_nr_w">
                	[cred_i18n name='landline_nr_w-label']
                			Landline nr (W)
                
                	[/cred_i18n]
              </label>
                  	[cred_field field='landline_nr_w' placeholder='Landline Work'  force_type='field' class='form-control' output='bootstrap']
          </div>
  
          <div class="form-group cell">
              
            	<label for="%%FORM_ID%%_cell-phone-number">
                  	[cred_i18n name='cell-phone-number-label']Cell Phone Number[/cred_i18n]
              	</label>
                  	[cred_field field='cell-phone-number' placeholder='Cell Nr (required)'  force_type='field' class='form-control' output='bootstrap']
          </div>
  
          <div class="form-group email">
              
            	<label for="%%FORM_ID%%_email">
                  	[cred_i18n name='email-label']Email[/cred_i18n]
              	</label>
                  	[cred_field field='email' force_type='field' placeholder='Email Address (required)'  class='form-control' output='bootstrap']
          </div>
    </div>
	
<!--end contentcontainer formating-->


<!--begin containerbutton formating-->	
  	
		<div class= "containerbutton">
			<div class="subbtn">
					<div class="backbtn">
                        <button class="backbutton" type="button" onclick="javascript: window.history.back();" class="button">
                                Back
                        </button>
            </div>
			
            <div class="nextbtn">
                    [cred_field field='form_submit' output='bootstrap' value='Next' class='btn btn-primary btn-lg']
             </div>	
		</div>
    </div>
	
<!--end containerbutton formating-->

</div>
  
  [/credform]

We will need to replicate this on more than one form, however, if i can figure out this one the rest should not be to complex

#2173849

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to compare the email that user registered with user registration form and check against the custom post type profile custom field email?

#2173921

Hi Minesh,
Thanks for your reply.

No, i actually want to compare the emails added or changed via a post form to the current email in the Custom Post type. In other words, I want to prevent duplicate entries of emails in the Custom post type field called Email.

#2173925

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where you added the form and admin access details as well as one user profile access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2174685

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the link where you added the form?

#2174705

Good Morning Minesh,

Here is the login information as requested. Please note this is a live site. If you do need to make any changes activate the under maintenance function.

Regards

#2174743

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the link of the page where you added your form?

#2174745

HI MInesh,

There are a few places I need it to activate.
the first one is
toolset post form
New Editors Form Step 1 id 1391
the second one is
Update Editors Profile Step 03 id 1440
Regards

#2174785

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes but on what post/page I can see the "New Editors Form " and "Update Editors Profile "?

#2174793

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the link of the page where you added your form?

#2174887

Hi Minesh,

Sorry about the misunderstanding.
New Editors Form Step 1 id 1391
is on the page
hidden link
The Update Editors Profile Step 03 id 1440
is on the page
hidden link

I however do need to point out that The profile completeness page and the add new member profile pages are both multipage forms. The first one the email field is visible from scratch,
the second one you will not be able to access the forms on the page due to the fact that a profile has not yet been created for the toolsetadmin user. On that page, the mail field is available on the third screen.

You may need to create a profile with the
hidden link page so that the profile completeness page will render.

Dont worry about the entry in the profile custom post type we will do a clean up once this matter has been resolved

#2174923

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately the admin access details you shared is not working at this end.

1) Can you please send e working admin access details.
2) A new user access details who is register and for which I can create a new profile hidden link

I have set the next reply to private which means only you and I have access to it.

#2175999

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Toolset offers the place where you can add your custom code at: Toolset => Settings => Custom Code tab

I've moved the filter code from functions.php file to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('cred_form_validate','func_validate_email',10,2);
function func_validate_email($error_fields, $form_data){
  
    //field data are field values and errors
  
    list($fields,$errors)=$error_fields;
  
     if ($form_data['id']==1391 or $form_data['id']==1440){
          
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-email',
                              'value' => $_POST['wpcf-email']
                            )),
                    'post_type' => 'editor-profile',
                    'posts_per_page' => -1
                    );
            $posts = get_posts($args);
  
  
        //check if birthday value is already on the database
  
        if (count($posts) > 0){
  
            //set error message for my_field
  
            $errors['wpcf-email']='This E-mail already registered.';
        } 
    } 
    return array($fields,$errors);
}

Can you please confirm it works as expected?

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