为什么我的联系人表格7字段验证也禁止空条目



我在functions.php中添加了两个字段的验证。这些验证了字段只能包含字母/数字,而不能包含特殊字符。它运行良好,但不幸的是,字段"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"]

在您的函数中,您正在验证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;
}

在此处使用官方文档进行自定义验证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;
}

相关内容

  • 没有找到相关文章

最新更新