• Resolved nimnim

    (@nimnim)


    Hi,

    I’m trying to prevent duplicate email submission to my database using the filter below, which I added in Shortcodes, Actions, and Filters plugin, and CFDB plugin.

    Everything works fine, i.e., once the email already exists in the database the user won’t be able to submit another form. The only problem is that when such an error occurred I still get the default validation error instead of the one I customized in the code below.

    Any help? I really appreciate it!
    thanks
    /**
    * @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 = ‘MembershipClub’;
    $fieldName = ‘m-email’;
    $errorMessage = ‘A user with this email address is already registered’;
    $name = $tag[‘name’];
    if ($name == $fieldName) {
    if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
    $result->invalidate($tag, $errorMessage);
    }
    }
    return $result;
    }

    add_filter(‘wpcf7_validate_email*’, ‘my_validate_email’, 10, 2);

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    The only problem is that when such an error occurred I still get the default validation error instead of the one I customized in the code below.

    What error exactly do you mean by “the default validation error”?

    Thread Starter nimnim

    (@nimnim)

    This is the error I get “Validation errors occurred. Please confirm the fields and submit it again.”, while I expect to get “A user with this email address is already registered”.

    Thanks a lot!
    Nima

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    What do you see at the email field?

    Thread Starter nimnim

    (@nimnim)

    At the email field I just enter my email address, that I have already used to register once to the database. Since the email address is already used the database does not allow me to register with it again. Hence, I should see an error message that I customized, but instead I get “Validation errors occurred. Please confirm the fields and submit it again.” in box with a yellow border below the submit button.

    But there is no specific error message in or around the email field itself, if that’s what you mean.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    OK. Where can I see the site?

    Thread Starter nimnim

    (@nimnim)

    Hi Takayuki

    Looks like I figured it out! There was actually a problem with my html coding that placed the header file in a wrong place. I cleaned up everything and it seems to be working now! It still shows the default error message at the bottom, but it also shows the customized message next to the relevant field, which is fine! I just need to do some CSS to bring it below the field!

    You can check out my test here: http://www.brandlandgroup.se/test/

    So no problem with contact form 7! It is really an awesome plugin! Thanks for the great work and also following up with me so promptly!

    Best regards
    Nima

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

The topic ‘Custom Validation Message Not Showing’ is closed to new replies.