Skip Navigation

[Resolved] Need to validate unique values in post field

This support ticket is created 5 years, 9 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 4 replies, has 2 voices.

Last updated by Minesh 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1201518

Tell us what you are trying to do?

Add validation to make sure that each posts created has unique string in a particular field.

I am using Types and Post Forms. I have created a set of custom fields for a custom post type called "SE Challenge Entries". These are posts created by a Post Form that contestants will fill out. The form gathers name, address, email, phone, etc., and allows that contestants to upload two image files required by the operator of the contest.

As a simple method of preventing duplicate entries by the same person, I want to add validation to the Email field that is part of the field group "SE Challenge Entry Fields". I understand that I can write my own validation to do this, but I think that it would be much easier if I had a simple check box in the validation area of the Field Group Editor for "Require unique" which would check all of the posts of this type to verify that there is not already a post by someone with this same email address. NOTE: These are not WordPress users. I am not bothering to create new users for each entry. I am just recording the visitor's email together with their form submitted contest entry.

I believe that, If this were a feature, this would be much simpler for Types & Post Form users.

If this is not something that can be accomplished simply, what is the PHP code required to validate that the email field being sent in the Post Form is not already in the database?

#1201756

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - Toolset Forms offers hook for validating the post before you save it. You can use Form hook cred_form_validate to validate your form.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

For example:

add_filter('cred_form_validate','func_validate_birthdate',10,2);
function func_validate_birthdate($error_fields, $form_data){
 
    //field data are field values and errors
 
    list($fields,$errors)=$error_fields;
 
     if ($form_data['id']==9999){
         
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-EMAIL-FIELD-SLUG',
                              'value' => $_POST['wpcf-EMAIL-FIELD-SLUG']
                            )),
                    'post_type' => 'your-post-type',
                    '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-FIELD-SLUG']='This E-mail already registered.';
 
        }
 
    }
 
    return array($fields,$errors);
 
}

Where:
- Replace 9999 with your original Form ID
- Replace EMAIL-FIELD-SLUG with original email field slug
- Replace your-post-type with your original post type

#1203408

Thanks, Minesh, that works great.

I also need to do a couple of specific things during the validation:

1) I need to make sure that emails entered via the form use one of two possible domains: domainone.com or domaintwo.com for example. minesh@domaintwo.com would be accepted but minesh@google.com would generate a form error. Can you help me with the PHP for this bit?

2) If someone successfully submits this form, I need to allow them to return and modify their entries in the future. I am NOT creating wordpress users with these emails. I am only storing email addresses in a custom post type. When a user returns to the site, I would like to automatically show them the field values for the form they submitted already and allow them to make any changes/additions.

If the validation code above, it looks like I could modify the part where the error for a pre-existing email could be modified to redirect the user to the edit existing post version of the same form. Perhaps it would make sense to break this into 2 steps; first, have the user enter their email address and then re-direct them to either a new entry form or edit existing form depending on whether the email is in the database or not.

If that is the best approach, I will need to make a new form that contains only the email address and then, in the validation for that form add a redirect in the validation code to either new entry or edit existing based on the email. I will also need to perform the validation to screen out any disallowed domains at that point.

Would it make more sense to add JavaScript to write either the email or the post ID of the submission to a cookie and use that to redirect the user to the edit version of the same form? Or, is there a preferred method for this?

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-how-to-validate-specific-email-domain-with-forms/

#1203559

Minesh
Supporter

Languages: English (English )

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

Well - would you mind to resolve this ticket. I will split the ticket with your each new question.

I'm taking care of validating the email domain with the split ticket. Please open a new ticket for #2 question.

This will help other users searching on the forum as well as help us to write correct ticket resolution summary to which original ticket belongs to.

#1207757

Minesh
Supporter

Languages: English (English )

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

Can you please mark resolve this ticket 🙂