Custom validation is not working in Contact Form 7

2019-05-08 18:40发布

I have to make a form with custom validation field in contact form 7. It is not working with latest version (4.1.1) of Contact Form 7 but working in older version.

I have created a field for getting coupon code from the form. I want to validate the entry if the coupon is started from "HIP". My code is given below:

add_filter( 'wpcf7_validate_text', 'your_validation_filter_func', 999, 2 );
add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func', 999, 2 );


function your_validation_filter_func( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];      
if ( 'coupon_code' == $name ) {
$the_value = $_POST[$name];

        $myresult = substr($the_value, 0, 3);
        if($myresult=="HIP")
        {
            $result['valid'] = true;
        }
        else
        {
            $result['valid'] = false;
            $result['reason'][$name] = "Not a valid coupon code";
        }
}

return $result;
}

Give me suggestion please.

3条回答
Anthone
2楼-- · 2019-05-08 19:20

I had a similar issue with contact form 7 custom validations. Finally landed on this post and also on official custom form 7 custom validations link here: http://contactform7.com/2015/03/28/custom-validation/.

The only update required for the code working on the earlier versions of CF7 is to replace the following line of code:

$result['reason'][$name] = 'Your custom validation message goes here';

with:

$result->invalidate( $tag, "Your custom validation message goes here." );
查看更多
Rolldiameter
3楼-- · 2019-05-08 19:33

    function custom_phone_validation($result,$tag){
        $type = $tag['type'];
        $name = $tag['name'];
        if($name == 'phonenumber'){
                    $phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
            $the_value = preg_match("/your_reg_exp format for phone number/",$_POST[$name]);
            if($phoneNumber == "" || $the_value == false ){ 
                $result->invalidate( $tag, "please enter vaild phone number" );
            }
        }
        return $result;
    }
    add_filter('wpcf7_validate_text','custom_phone_validation', 10, 2);
    add_filter('wpcf7_validate_text*', 'custom_phone_validation', 10, 2);

phonenumber -> field name

try this once.

查看更多
SAY GOODBYE
4楼-- · 2019-05-08 19:42

I have also this issue was occurred when i update the contact form 7 with 4.1.1. In the latest version of contact form 7 old custom validation code is not working.

So after very research i have found solution for this. So in your case you need to change in your code as following. May be it will be helpful for you.

add_filter('wpcf7_validate_text', 'your_validation_filter_func', 999, 2);
add_filter('wpcf7_validate_text*', 'your_validation_filter_func', 999, 2);

function your_validation_filter_func($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];

if ('coupon_code' == $name) {

    $the_value = $_POST[$name];

    $myresult = substr($the_value, 0, 3);
    if ($myresult == "HIP") {
        $result['valid'] = true;
    } else {
        $result->invalidate($tag, wpcf7_get_message('invalid_coupon_code'));
    }
}

return $result;
}

add_filter('wpcf7_messages', 'mywpcf7_text_messages');

function mywpcf7_text_messages($messages) {
return array_merge($messages, array(
    'invalid_coupon_code' => array(
        'description' => __("Coupon is invalid", 'contact-form-7'),
        'default' => __('Coupon seems invalid.', 'contact-form-7')
)));
}
查看更多
登录 后发表回答