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?
Hello. Thank you for contacting the Toolset support.
Well - to validate the specific email domain, you should try to use the following function:
For example - use the following function:
function validate_email_domain($email){
if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;
$domains = array('gmail.com','yahoo.com','hotmail.com');
list(, $email_domain) = explode('@', $email, 2);
return !in_array($email_domain, $domains);
}
add_filter('cred_form_validate','func_validate_email_with_domain',10,2);
function func_validate_email_with_domain($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;
if ($form_data['id']==9999){
$is_valid_email_domain = validate_email_domain($_POST['wpcf-EMAIL-FIELD-SLUG']);
if($is_valid_email_domain) {
$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.';
}
}else{
$errors['wpcf-EMAIL-FIELD-SLUG']='This E-mail domain is not allowed.';
}
}
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
- Replace the $domains with the values of domains which you want to allow.
Thank you Minesh. It works for finding duplicate emails, but it does not correctly limit the domains to those in the array.
I have a custom post type of Filtered Email and a custom field for that post type called Domain Limited Email. The slug appears to work as the check for existing record part of the code functions correctly.
Here's the actual code I'm using:
function validate_email_domain($email){
if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;
$domains = array('domain1.com','domain2.com','domain3.biz');
list(, $email_domain) = explode('@', $email, 2);
return !in_array($email_domain, $domains);
}
add_filter('cred_form_validate','func_validate_email_with_domain',10,2);
function func_validate_email_with_domain($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;
if ($form_data['id']==214){
$is_valid_email_domain = validate_email_domain($_POST['wpcf-domain-limited-email']);
if($is_valid_email_domain) {
$args = array(
'meta_query' => array(
array('key' => 'wpcf-domain-limited-email',
'value' => $_POST['wpcf-domain-limited-email']
)),
'post_type' => 'filtered-email',
'posts_per_page' => -1
);
$posts = get_posts($args);
//check if email is already in the database
if (count($posts) > 0){
//set error message for my_field
$errors['wpcf-domain-limited-email']='This E-mail already registered. Please use the link you received via email to update your contest entry.';
}
}else{
$errors['wpcf-domain-limited-email']='Not an authorized, work email.';
}
}
return array($fields,$errors);
}
If I submit an entry using either bob@domain1.com or bob@domain4.com they both return success. Perhaps there is an error with the line:
My client has updated this site and now wishes to add a large number of approved email domains. Isn't there a way to do validation within Toolset, using Toolset logic? I need to be able to create a custom post type for Approved Domains, which the client can edit and maintain.
Then, I need to be able to redirect entries based on whether their email domain is one of these custom post types or not. It would certainly be great if there was a validation editor for forms, similar to that for views. Is there any way to obtain this functionality that would preclude writing any custom PHP functions?
New threads created by Minesh and linked to this one are listed below: