我只需要验证一个字段(称为“实例”)以仅接受小写 ASCII 字母和数字,第一个字符也必须是字母而不是数字。它将接受大写字符,但我们需要它在输入时将它们小写。因此,如果有人使用实例名称 McDonalds,它将被小写为 mcdonalds(不只是使用 CSS)。也不允许有空格。
这在 CF7 中可行吗?如果是,请解释如何。
我已经试过了 this自定义验证方法,但即使在文件中使用预设的自定义验证,它也只是显示字段简码而不是字段本身。
谢谢
最佳答案
来自 contactform7.com 自定义验证 → 作为过滤器验证:
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; }
Two parameters will be passed to the filter function:
$result
and$tag
.$result
is an instance ofWPCF7_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 useWPCF7_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
).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 theinvalidate()
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
.
关于php - 联系表格 7 - 自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858288/