• Resolved Greg_R

    (@greg_r)


    I’ve been trying for a few hours to make this Prevent Duplicates code work ‘in reverse’ – https://cfdbplugin.com/?page_id=904

    In other words, instead of checking for duplicates, I have an existing database of email addresses that are allowed senders. Any others should be considered invalid. Logically, it should be a simple matter of finding the right place to change “==” to “!=” in your Prevent Duplicates code, but unfortunately, that’s not the case. How can I make this work?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    I think you would just change this:
    if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
    to this:
    if (! is_already_submitted($formName, $fieldName, $_POST[$name])) {

    Thread Starter Greg_R

    (@greg_r)

    Thanks Michael! I’ll give this a try and report back shortly.

    Thread Starter Greg_R

    (@greg_r)

    Here is what I have, but it doesn’t fail the validation or prevent submission when I enter a non-existing email:

    
    /**
     * @param $formName string
     * @param $fieldName string
     * @param $fieldValue string
     * @return bool
     */
    function is_already_submitted($formName, $fieldName, $fieldValue) {
        require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
        $exp = new CFDBFormIterator();
        $atts = array();
        $atts['show'] = $fieldName;
        $atts['filter'] = "$fieldName=$fieldValue";
        $atts['unbuffered'] = 'true';
        $exp->export($formName, $atts);
        $found = false;
        while ($row = $exp->nextRow()) {
            $found = true;
        }
        return $found;
    }
     
    /**
     * @param $result WPCF7_Validation
     * @param $tag array
     * @return WPCF7_Validation
     */
    function my_validate_email($result, $tag) {
        $formName = 'rebate_reg'; // Change to name of the form containing this field
        $fieldName = 'your-email'; // Change to your form's unique field name
        $errorMessage = 'You are not in our database of Prize Giveaway entries.'; // Change to your error message
        $name = $tag['name'];
        if ($name == $fieldName) {
            if (! is_already_submitted($formName, $fieldName, $_POST[$name])) {
                $result->invalidate($tag, $errorMessage);
            }
        }
        return $result;
    }
     
    // use the next line if your field is a **required email** field on your form
    add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
    // use the next line if your field is an **email** field not required on your form
    add_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);
     
    // use the next line if your field is a **required text** field
    add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);
    // use the next line if your field is a **text** field field not required on your form 
    add_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);
    • This reply was modified 7 years, 11 months ago by Greg_R.
    Thread Starter Greg_R

    (@greg_r)

    Oops, nevermind! Instead of the fieldname ‘your-email’, I had changed it to ‘reg-email’, and now that I corrected, it works as intended. Thanks for the help Michael!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Prevent submission if email *does not* exist?’ is closed to new replies.