联系表格7-自定义验证 [英] Contact Form 7 - Custom Validation

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

问题描述

我只需要验证一个字段(称为实例")以仅接受小写ASCII字母和数字,第一个字符也必须是字母而不是数字.它将接受大写字符,但我们需要在输入时将其小写.因此,如果有人使用实例名称McDonalds,它将被小写为mcdonalds(不仅是CSS).也不允许使用空格.

I need to validate just one field (called 'Instance') to accept lowercase ASCII letters and numbers only, the first character also has to be a letter not a number. It will accept uppercase characters but we will need it to lowercase them on input. So if someone uses the instance name McDonalds it will be lowercased to mcdonalds (not just with CSS). Spaces are not allowed either.

CF7可以吗?如果是这样,请解释如何做.

Is this possible with CF7? If so please explain how.

我已经尝试过自定义验证方法,但即使在文件中预设了自定义验证,它也只是显示字段简码,而不是字段本身.

I've already tried this custom validation method but even with the preset custom validation in the file it was just displaying the field shortcode rather than the field itself.

谢谢

推荐答案

来自 contactform7.com 上的自定义验证→验证为过滤器:

在联系表7中,用户输入的验证被实现为过滤器功能.验证所使用的过滤器钩根据不同而有所不同表单标签的类型,确定为:wpcf7_validate_ + {表单标签}.因此,对于文本表单标签,过滤器挂钩使用wpcf7_validate_text.同样,使用wpcf7_validate_email *用于电子邮件*表单标签.

In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}. So, for text form-tags, the filter hook wpcf7_validate_text is used. Likewise, wpcf7_validate_email* is used for email* form-tags.

假设您在表单中具有以下电子邮件字段:

Let’s say you have the following email fields in a form:

  Email:         [email* your-email]
  Confirm email: [email* your-email-confirm]

下面的清单显示了验证两个字段是否存在的代码具有相同的值.

The following listing shows code that verifies whether the two fields have identical values.

add_filter('wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2);

function custom_email_confirmation_validation_filter($result, $tag) {
    $tag = new WPCF7_FormTag($tag);

    if ('your-email-confirm' == $tag->name) {
        $your_email = isset($_POST['your-email']) ? trim($_POST['your-email']) : '';
        $your_email_confirm = isset($_POST['your-email-confirm']) ? trim($_POST['your-email-confirm']) : '';

        if ($your_email != $your_email_confirm) {
            $result->invalidate($tag, "Are you sure this is the correct address?");
        }
    }
    return $result;
}

两个参数将传递给过滤器函数: $ result $ tag . $ result WPCF7_Validation 类的实例,该类管理验证过程的顺序. $ tag 是一个关联数组由给定的表单标签组件组成;正如您在上一期中看到食谱,您可以使用 WPCF7_FormTag 类来处理此类数据.

Two parameters will be passed to the filter function: $result and $tag. $result is an instance of WPCF7_Validation class that manages a sequence of validation processes. $tag is an associative array composed of given form-tag components; as you saw in the previous recipe, you can use WPCF7_FormTag class to handle this type of data.

浏览过滤器功能的内部.首先,检查名称表单标签的名称,以确保仅将验证应用于特定字段(您的电子邮件确认).

Look through the inside of the filter function. First, check the name of the form-tag to ensure the validation is applied only to the specific field (your-email-confirm).

然后比较两个电子邮件字段的值,如果它们不匹配, $ result-> invalidate()将被调用.您需要传递两个参数到 invalidate()方法:第一个参数应该是 $ tag 变量,第二个参数是验证错误消息您希望该字段显示.

The two email field values are then compared, and if they don’t match, $result->invalidate() will be called. You need to pass two parameters to the invalidate() method: the first parameter should be the $tag variable, and the second parameter is the validation error message that you want the field to display.

最后,不要忘记返回 $ result .

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

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