【问题标题】:Custom Contact Form 7 Validation - One of two fields required自定义联系表 7 验证 - 两个必填字段之一
【发布时间】:2021-10-02 15:33:43
【问题描述】:

我的联系表单上有两个文本字段:成本加成和统一费率。 我需要两者之一。如果未输入成本加成,则需要统一费率.. 反之亦然。 functions.php 中的这段代码,似乎没有给我任何反馈。

add_filter( 'wpcf7_validate_text', 'custom_form_validation_filter', 20, 2 );
  
function custom_form_validation_filter( $result, $tag ) {
    $tag = new WPCF7_FormTag($tag);
    
    if ('cost-plus' == $tag->name) {
        $cost_plus = isset($_POST['cost-plus']) ? trim($_POST['cost-plus']) : '';
        $flat_rate = isset($_POST['flate-rate']) ? trim($_POST['flate-rate']) : '';

        if ( empty($cost_plus) && empty($flat_rate) ) {
            $result->invalidate( $tag, "You must enter a Cost Plus OR Flat Rate value." );
        }
    }
  return $result;
}

【问题讨论】:

    标签: php wordpress validation filter contact-form-7


    【解决方案1】:

    下面的代码应该适用于您的用例。也许您需要一些额外的检查,具体取决于前端发布请求中发送的值。如果下面的代码不适用于您的用例,请考虑在此处发布整个 POST 请求以进行调试。

    if(isset($_POST['cost-plus']) || isset($_POST['cost-plus'])){
        // Do logic here
    }else{
        $result->invalidate( $tag, "You must enter a Cost Plus OR Flat Rate value." );
    }
    

    【讨论】:

    • 还是不行.. CF7 POST 请求我没有改,所以是默认代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2019-03-28
    • 2014-05-16
    • 2015-03-08
    • 1970-01-01
    • 2017-08-06
    • 2011-06-16
    相关资源
    最近更新 更多