在4.1.1版中的联系表7中,自定义验证不起作用 [英] Custom validation is not working in Contact Form 7 in ver 4.1.1

查看:38
本文介绍了在4.1.1版中的联系表7中,自定义验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在联系表单7中创建一个带有自定义验证字段的表单。它不能与最新版本(4.1.1)的联系表单7一起使用,而只能在旧版本中使用。

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.

我创建了一个用于从表单获取优惠券代码的字段。如果优惠券从 HIP开始,我想验证输入。我的代码如下:

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;
}

请给我建议。

推荐答案

我在联系表单7自定义验证中遇到了类似的问题。最终登陆这篇文章以及官方自定义表单7自定义验证链接,网址为: http://contactform7.com/ 2015/03/28 / custom-validation /

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/.

在较早版本的CF7上运行的代码所需的唯一更新是替换以下行代码:

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';

具有:

$result->invalidate( $tag, "Your custom validation message goes here." );

这篇关于在4.1.1版中的联系表7中,自定义验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆