php 联系表格 7 - 自定义验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22858288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Contact Form 7 - Custom Validation
提问by mazecreative
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.
我只需要验证一个字段(称为“实例”)以仅接受小写 ASCII 字母和数字,第一个字符也必须是字母而不是数字。它将接受大写字符,但我们需要它在输入时将它们小写。因此,如果有人使用实例名称 McDonalds,它将被小写为 mcdonalds(不仅仅是使用 CSS)。也不允许有空格。
Is this possible with CF7? If so please explain how.
这可以用 CF7 实现吗?如果是这样,请解释如何。
I've already tried thiscustom validation method but even with the preset custom validation in the file it was just displaying the field shortcode rather than the field itself.
我已经尝试过这种自定义验证方法,但即使在文件中使用预设的自定义验证,它也只是显示字段短代码而不是字段本身。
Thanks
谢谢
回答by Zafar S
From contactform7.comon Custom Validation → Validation as a Filter:
从contactform7.com上的Custom Validation → Validation as a Filter:
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_Shortcode($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; }
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_Shortcode 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).
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.
Lastly, don't forget to return the $result.
在 Contact Form 7 中,用户输入验证是作为过滤器功能实现的。用于验证的过滤器钩子取决于表单标签的类型,确定为:wpcf7_validate_ + {表单标签的类型}。因此,对于文本表单标签,使用过滤器钩子 wpcf7_validate_text。同样, wpcf7_validate_email* 用于 email* 表单标签。
假设您在表单中有以下电子邮件字段:
Email: [email* your-email] Confirm email: [email* your-email-confirm]
下面的清单显示了验证两个字段是否具有相同值的代码。
add_filter('wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2); function custom_email_confirmation_validation_filter($result, $tag) { $tag = new WPCF7_Shortcode($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_Shortcode 类来处理此类数据。
查看过滤器功能的内部。首先,检查表单标签的名称以确保验证仅应用于特定字段(您的电子邮件确认)。
然后比较两个电子邮件字段值,如果它们不匹配,将调用 $result->invalidate()。您需要向 invalidate() 方法传递两个参数:第一个参数应该是 $tag 变量,第二个参数是您希望该字段显示的验证错误消息。
最后,不要忘记返回 $result。
回答by Mosab Muhammad
You can add your own custom validation for a form field input by using the add_filter
function.
您可以使用该add_filter
函数为表单字段输入添加您自己的自定义验证。
For adding a custom validation for a textarea
field you can add the following inside functions.php
file in the root directory of your theme.
要为textarea
字段添加自定义验证,您可以functions.php
在主题的根目录中添加以下内部文件。
add_filter( 'wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 1, 2 );
function custom_textarea_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode($tag);
$result = (object)$result;
$name = 'project-synopsis';
if ( $name == $tag->name ) {
$project_synopsis = isset( $_POST[$name] ) ? trim( wp_unslash( (string) $_POST[$name] ) ) : '';
if ( empty( $project_synopsis ) ) {
$result->invalidate( $tag, "Please write a quick project synopsis." );
}
}
return $result;
}
For me the trick was to cast the $result
parameter to an object, because the invalidate
method that is used to add the error message didn't work before casting.
对我来说,诀窍是将$result
参数转换为对象,因为invalidate
用于添加错误消息的方法在转换之前不起作用。
回答by formygalaxy
Please use this wordpress plugin
请使用这个wordpress插件
Jquery Validation For Contact Form 7 https://wordpress.org/plugins/jquery-validation-for-contact-form-7/
联系表格 7 的 Jquery 验证 https://wordpress.org/plugins/jquery-validation-for-contact-form-7/
回答by Vidhu
I had a similar issue for validating name fields, I added the following code in my functions.php, you could customize it by changing the regex
我在验证名称字段时遇到了类似的问题,我在我的functions.php 中添加了以下代码,您可以通过更改正则表达式来自定义它
function my_wpcf7_validate_text( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];
$value = $_POST[$name] ;
if ( strpos( $name , 'name' ) !== false ){
$regex = '/^[a-zA-Z]+$/';
$Valid = preg_match($regex, $value, $matches );
if ( $Valid > 0 ) {
} else {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_name' ) );
}
}
return $result;
}
add_filter( 'wpcf7_validate_text*', 'my_wpcf7_validate_text' , 10, 2 );
add_filter( 'wpcf7_messages', 'mywpcf7_text_messages' );
function mywpcf7_text_messages( $messages ) {
return array_merge( $messages, array(
'invalid_name' => array(
'description' => __( "Name is invalid", 'contact-form-7' ),
'default' => __( 'Name seems invalid.', 'contact-form-7' )
)
));
}
回答by Raks
// Add custom validation for CF7 form fields
// 为 CF7 表单字段添加自定义验证
function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them
if(
preg_match('/@gmail.com/i', $email) ||
preg_match('/@hotmail.com/i', $email) ||
preg_match('/@live.com/i', $email) ||
preg_match('/@msn.com/i', $email) ||
preg_match('/@aol.com/i', $email) ||
preg_match('/@yahoo.com/i', $email) ||
preg_match('/@inbox.com/i', $email) ||
preg_match('/@gmx.com/i', $email) ||
preg_match('/@me.com/i', $email)
){
return false; // It's a publicly available email address
}else{
return true; // It's probably a company email address
}
}
function your_validation_filter_func($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if('yourid' == $value){ // Only apply to fields with the form field name of "company-email"
$the_value = $_POST[$name];
if(!is_company_email($the_value)){ // Isn't a company email address (it matched the list of free email providers)
$result['valid'] = false;
$result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ));
}
}
return $result;
}
add_filter( 'wpcf7_validate_email', 'your_validation_filter_func', 10, 2 );
// Email field or contact number field
add_filter( 'wpcf7_validate_email*', 'your_validation_filter_func', 10, 2 ); // Req. Email field or contact number