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?
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
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: