为什么我的 Contact Form 7 字段验证也禁止空条目?
Why is my Contact Form 7 field validation prohibiting empty entries too?
我在 functions.php 中为两个字段添加了验证。这些验证该字段只能采用 letters/numbers 并且没有特殊字符。它工作正常,但不幸的是字段 'tussenvoegsel' 不能留空,这也需要是可能的。
// VALIDATIE INPUT FIELDS (GEEN CIJFERS MOGELIJK ALS INPUT)
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'familienaam' == $tag->name ) {
$name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
if ( 'tussenvoegsel' == $tag->name ) {
$name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
return $result;
}
这很奇怪,因为 'tussenvoegsel' 的插件中的条目没有 *。
我可以添加什么来保持 'tussenvoegel' 条目仍然是可选的,即使我也需要验证。
[text tussenvoegsel placeholder "voorvoegsel"]
[text* familienaam placeholder "Achternaam"]
此处使用官方文档进行自定义验证https://contactform7.com/2015/03/28/custom-validation/
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
if ( 'familienaam' == $tag->name ) {
$familienaam= isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
if ( !ctype_alnum( $familienaam) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
if ( 'tussenvoegsel' == $tag->name ) {
$tussenvoegsel = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
if ( !ctype_alnum( $tussenvoegsel ) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
return $result;
}
在您的函数中,您正在验证 CF7 字段,但没有检查该字段是否为必需字段。在这种情况下,它将始终 运行 您的功能通过验证过滤器,并且无论是否需要该字段都要求输入是字母数字。
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'familienaam' === $tag->name ) {
$name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
// Check if the field is empty or required.
if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
if ( !preg_match('/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
$result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
}
}
}
if ( 'tussenvoegsel' === $tag->name ) {
$name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
// Check if the field is empty or required.
if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
$result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
}
}
}
return $result;
}
我在 functions.php 中为两个字段添加了验证。这些验证该字段只能采用 letters/numbers 并且没有特殊字符。它工作正常,但不幸的是字段 'tussenvoegsel' 不能留空,这也需要是可能的。
// VALIDATIE INPUT FIELDS (GEEN CIJFERS MOGELIJK ALS INPUT)
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'familienaam' == $tag->name ) {
$name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
if ( 'tussenvoegsel' == $tag->name ) {
$name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
return $result;
}
这很奇怪,因为 'tussenvoegsel' 的插件中的条目没有 *。 我可以添加什么来保持 'tussenvoegel' 条目仍然是可选的,即使我也需要验证。
[text tussenvoegsel placeholder "voorvoegsel"]
[text* familienaam placeholder "Achternaam"]
此处使用官方文档进行自定义验证https://contactform7.com/2015/03/28/custom-validation/
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
if ( 'familienaam' == $tag->name ) {
$familienaam= isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
if ( !ctype_alnum( $familienaam) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
if ( 'tussenvoegsel' == $tag->name ) {
$tussenvoegsel = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
if ( !ctype_alnum( $tussenvoegsel ) ) {
$result->invalidate( $tag, "Alleen letters zijn toegestaan." );
}
}
return $result;
}
在您的函数中,您正在验证 CF7 字段,但没有检查该字段是否为必需字段。在这种情况下,它将始终 运行 您的功能通过验证过滤器,并且无论是否需要该字段都要求输入是字母数字。
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
function alphanumeric_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'familienaam' === $tag->name ) {
$name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
// Check if the field is empty or required.
if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
if ( !preg_match('/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
$result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
}
}
}
if ( 'tussenvoegsel' === $tag->name ) {
$name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
// Check if the field is empty or required.
if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
$result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
}
}
}
return $result;
}