• Resolved christieprimier

    (@christieprimier)


    Mike – It’s me again 😀 Doing regression testing. In our database we have it so that a user can’t put it more than one team. I can send code if you need to see. My question instead of applying it to the whole site is there a way that we can apply it to just one form? Cause right now It’s being applied to the team change form as well, and I’m not sure how to get rid of it.

    Thank you SOO much
    Christie

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

    (@msimpson)

    Sorry, I don’t recall the context of your earlier thread (too many for me to keep track of). Apply what? Can you provide more context or link to an earlier thread?

    Thread Starter christieprimier

    (@christieprimier)

    Mike,
    Sorry, You’re right that was mean of me.
    https://wordpress.org/support/topic/duplicate-missing-something-easy-help-appreciated/ that’s the old thread and it works 🙂 I just don’t want it to work for every time I put in a team name or email address. I only want it for that one form.

    Thanks in advance, you’re the best

    Thread Starter christieprimier

    (@christieprimier)

    Mike,
    The above post…clarified. The process works amazing. I just want it to be specific to one form, not the whole site. That’s what I was trying to say

    Plugin Author Michael Simpson

    (@msimpson)

    I think we are talking about preventing duplicate submissions using CF7 hooks:
    http://cfdbplugin.com/?page_id=904

    As I explain in that page, there is a limitation where the CF7 hook doesn’t tell us what form the validation is associated with. So to make it work with only one form, you need to use a unique field name that only appears on that form and no other. In the example I use “email_123”.

    Thread Starter christieprimier

    (@christieprimier)

    Mike,
    That didn’t work 🙁 But I’m still working on it 🙂
    Thank you

    Thread Starter christieprimier

    (@christieprimier)

    Mike,
    It isn’t working at all :(. I’m not sure exactly where I am going wrong.
    Sorry to be a pest. There is one point where I can’t even submit a form the first time.

    Thanks in advance
    Christine

    Plugin Author Michael Simpson

    (@msimpson)

    Post your code. Format it with the “code” button.

    Thread Starter christieprimier

    (@christieprimier)

    /**
    * @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 = ‘Team_Form’; // Change to name of the form containing this field
    $fieldName = ‘Team_Validate’; // Change to your form’s unique field name
    $errorMessage = ‘Email has already been submitted’; // Change to your error message
    $name = $tag[‘name’];
    if ($name == $fieldName) {
    if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
    $result->invalidate($tag, $errorMessage);
    }
    }
    $fieldName = ‘Submitted Login’;
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
    // Not logged in.
    $result->invalidate($tag, ‘Not logged in’);
    } else {
    // Logged in.
    $fieldValue = $current_user->user_login ;
    if (is_already_submitted($formName, $fieldName, $fieldValue)) {
    $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);

    That’s the code and it touches everything. I’m not sure how to fix it. Thank you 🙂

    Plugin Author Michael Simpson

    (@msimpson)

    It is hard to read when you don’t format it as “code” like I asked, and I can’t copy your code b/c the quotes are messed up by the not-code formatting.

    If your field is a CF7 email field, then you need these lines:

    add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
    add_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);

    Not these lines:

    add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);
    add_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);
    Thread Starter christieprimier

    (@christieprimier)

    /**
     * @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 = 'Team_Form'; // Change to name of the form containing this field
        $fieldName = 'Team_Validate'; // Change to your form's unique field name
        $errorMessage = 'Email has already been submitted'; // Change to your error message
        $name = $tag['name'];
        if ($name == $fieldName) {
            if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
                $result->invalidate($tag, $errorMessage);
            }
        }
    	$fieldName = 'Submitted Login';
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
        // Not logged in.
         $result->invalidate($tag, 'Not logged in');
    } else {
        // Logged in.
       $fieldValue = $current_user->user_login ;
       if (is_already_submitted($formName, $fieldName, $fieldValue)) {
                $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);

    Sorry I forgot 🙁 I did that and it still happens 🙁 Is there someplace else that it sits that I’m not finding?

    Plugin Author Michael Simpson

    (@msimpson)

    Your my_validate_email function has extra stuff in there. This function runs for all email fields. So in the function, you want all code that checks & rejects anything to be inside the “if” statement that check the field name (and that field name should only be on one form.

    That function should just be this:

    function my_validate_email($result, $tag) {
        $formName = 'Team_Form'; // Change to name of the form containing this field
        $fieldName = 'Team_Validate'; // Change to your form's unique field name
        $errorMessage = 'Email has already been submitted'; // 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;
    }
    Thread Starter christieprimier

    (@christieprimier)

    Mike, That did it 🙂 Thank you SO much. I had to work on finding why the error messages kept coming up. SO I figured it out. Now to make everything else work 🙂 – Thank you so much for everything!

    Christie

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Addon,fuction,filter questions’ is closed to new replies.