• Resolved scamrwordpressadmin

    (@scamrwordpressadmin)


    We use contact forms to allow our hiking club members to sign up for hikes. Each hike has its’ own form that ties to a specific event (date & time). This works like a charm.

    We added the ability to filter a submission by the user-name, via the “Add Shortcodes Actions and Filters” Plug in. (We allow multiple users to share email addresses (ie husband and wife) – so need to check the sign up via user name. )

    This works – however along with the message that a user is already logged in, I get a validation error message on the user name.

    Here is the CFDB filter (via the plug in):

    /**
     * @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 = 'San Tan - Moonlight Trail (Short) - 2 Nov 2016'; // Change to name of the form containing this field
        $fieldName = 'your-name'; // Change to your form's unique field name
        $errorMessage = 'You have already registered for this hike.'; // 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 text** field
    add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);

    Questions:

    1. How to stop the validation error from showing up when a user tries to register more than once?

    2. If the filter is set to look a specific form name, why does it hit anytime someone tries to register for a hike? ie it will tell me I have already signed up for a hike when I haven’t.

    Thanks for any thoughts and pointers.

    Cheers

Viewing 1 replies (of 1 total)
  • Thread Starter scamrwordpressadmin

    (@scamrwordpressadmin)

    Resolution:

    1. I just deleted the validation error text. This stops the validation error from showing up. However, if I wanted to validate against properly formed phone #s or something, I could have a problem. For now, this workaround will suffice.

    2. I had to create a unique $fieldName for each contact form (hike sign up) and then create a unique filter for each hike, using that same $fieldName. I had to duplicate each for the unsubscribe function – again with unique $fieldNames. The function names also had to be unique, as do the filter names.

    With a unique form & filter for each sign up and each unsubscribe (per hike), this works. But that’ll be upwards of 300 forms/filters for sign up and 300 forms/filters for unsubscribe in a year. Lots of coding there, and not very maintenance friendly.

    That said, the filters, when coded this way, work like a charm.

Viewing 1 replies (of 1 total)
  • The topic ‘[Contact-Form 7] Email Validation error shows up incorrectly’ is closed to new replies.