【问题标题】:CF7 - Database field validationCF7 - 数据库字段验证
【发布时间】:2017-05-26 09:10:25
【问题描述】:

我想使用数据库中的自定义列表验证我的 cf7 表单。因此,如果用户在字段中输入的值不在我的列表中,则表单将无法通过验证。这是一个包含 60,000 个条目的长列表。

感谢您的帮助!

【问题讨论】:

  • 我建议在wordpress.stackexchange.com 上询问这个问题并添加一些关于列表的信息。此列表的存储位置/方式。

标签: php mysql wordpress contact-form-7


【解决方案1】:

您可以使用 cf7 的自定义验证钩子。您将需要执行以下操作:

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;
}

这是对电子邮件字段的基本自定义验证,您需要像这样自定义:

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

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

global $wpdb;
$searchTerm = $wpdb->get_row('select * from ' . $wpdb->prefix . 'name_of_table where name_of_field = "' . $_POST['given_value'] . '"');

if(!$searchTerm)
    $result->invalidate( $tag, "Value doesn't match" );

return $result;
}

如果您使用文本字段,您也可以使用 wpcf7_validate_text 代替 wpcf7_validate_mail

【讨论】:

  • 谢谢,这很好 - 但是,它现在适用于我的所有表格。有没有办法将上述代码修改为仅适用于一个 CF& 表单?
  • 您能否在调用过滤器add_action 时引入一个控件。如果您只想在给定页面上应用此过滤器,您可以这样做: global $post; if($post->ID == 1) add_filter('wpcf7_validate_email*', 'custom_field_validation', 20, 2);其中 1 是您的帖子或页面 ID
  • 我正在努力获取帖子 ID,因此使用 url 检查字符串。然后将整个函数包装在 if 语句中。感谢您的帮助!
猜你喜欢
  • 2012-05-12
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多