【问题标题】:Custom validation message for select field in Contact form 7 WordPress联系表单 7 WordPress 中选择字段的自定义验证消息
【发布时间】:2019-08-30 09:34:29
【问题描述】:

我想为选择字段显示自定义消息。我正在为自定义消息使用插件“Contact form 7 Custom validation”,但它不适用于选择字段。是否有一个钩子可以仅针对选择字段修改我的消息,因为我的验证消息的其余部分都很好。

更新:

我有以下字段:

<div class="form-half">
                              <label for="state" class="visuallyhidden">state</label>[select* state id:state first_as_label "State" "Alabama" "Alaska" "American Samoa" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" "District of Columbia" "Florida" "Georgia" "Guam" "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa" "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland" "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri" "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey" "New Mexico" "New York" "North Carolina" "North Dakota" "Northern Marianas Islands" "Ohio" "Oklahoma" "Oregon" "Pennsylvania" "Puerto Rico" "Rhode Island" "South Carolina" "South Dakota" "Tennessee" "Texas" "Utah" "Vermont" "Virginia" "Virgin Islands" "Washington" "West Virginia" "Wisconsin" "Wyoming"]</div>

我已经使用了以下钩子来做到这一点,但不起作用:

add_filter( 'wpcf7_validate_select*', 'custom_select_validation_filter', 20, 2 );

function custom_select_validation_filter( $result, $tag ) {
    if ( 'state' == $tag->name ) {

        echo $test_custom_select = $_POST['state'];
        if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
            // Example of result
            $result->invalidate($tag, __( 'Please enter state name', 'CF7' ));
        }

    }

    return $result;
}

但这不起作用。

【问题讨论】:

    标签: wordpress contact-form-7


    【解决方案1】:

    试试下面的代码:

    add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );
    
    
    function custom_select_validation_filter( $result, $tag ) {
        if ( 'state' == $tag->name ) {
    
            $test_custom_select = $_POST['state'];
            if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
                // Example of result
                $result->invalidate($tag, __( 'your-select is required', 'CF7' ));
            }
    
        }
        elseif ( 'second-select' == $tag->name ){
    
            $test_custom_select = $_POST['second-select'];
            if ( empty( $test_custom_select ) ) {
                // Example of result
                $result->invalidate($tag, __( 'second-select is required', 'CF7' ));
            }
    
        }
    
        return $result;
    }
    

    https://contactform7.com/2015/03/28/custom-validation/

    字段 CF7: [select state id:state first_as_label "State" "Alabama" "Alaska" "American Samoa" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" "District of Columbia" "Florida" "Georgia" "关岛“夏威夷”“爱达荷”“伊利诺伊”“印第安纳”“爱荷华”“堪萨斯”“肯塔基”“路易斯安那”“缅因”“马里兰”“马萨诸塞”“密歇根”“明尼苏达”“密西西比”“密苏里”“蒙大拿” “内布拉斯加州” “内华达” “新罕布什尔” “新泽西” “新墨西哥” “纽约” “北卡罗来纳” “北达科他” “北马里亚纳群岛” “俄亥俄” “俄克拉荷马” “俄勒冈” “宾夕法尼亚” “波多黎各” ““罗德岛”“南卡罗来纳”“南达科他”“田纳西”“德克萨斯”“犹他”“佛蒙特”“弗吉尼亚”“维尔京群岛”“华盛顿”“西弗吉尼亚”“威斯康星”“怀俄明”]

    【讨论】:

    • 验证满意,我只想改消息
    • 还有2到3个select字段,所以每个字段应该有不同的信息
    • 您可以在此功能中检查选择字段并检查是否选择了选项。如果没有,您可以返回自定义消息。我将编辑我的代码。
    • 嗨,我已经更新了我的代码,你可以指导我,如果我做的是对还是错?
    • 嗨@AlWaqar,抱歉在add_filter 中不需要*(我已经更新了代码)。您的字段也不需要是必需的。
    【解决方案2】:

    经过测试并且可以工作。

    // For the custom Price for shuttle transport
    /**
     * Generates a HTML string of two or more `option` elements/tags.
     *
     * @see wpcf7_select_shuttleprice_form_tag_handler()
     *
     * @return string $html
     */
    function shuttleprice() {
    
        $id_a = null;      
        $max_personen = get_field("maximale_anzahl", $id_a);
        $max_personen_gesamt = get_field("anzahl_maximale_personen_im_shuttle_mit_aufpreis", $id_a);
        $aufpreis = get_field("aufpreis_pro_person_im_shuttle", $id_a);
    
        $inkl = "";
        $more = "";
    
        for ($x = 1; $x <= $max_personen; $x++) {
            if($x == 1) {
                $inkl = $inkl."<option value='".$x."'>für ".$x." Person (inklusive)</option>";
            } else {
                $inkl = $inkl."<option value='".$x."'>für ".$x." Personen (inklusive)</option>";
            }
        }
    
        if($max_personen_gesamt != "") {
            $lauf = 1;
            for ($x = $max_personen + 1; $x <= $max_personen_gesamt; $x++) {
                $more = $more.'<option data-price="'.$aufpreis*$lauf.'" value="'.$x.'">für '.$x.' Personen ('.$aufpreis*$lauf.' € Aufpreis)</option>';
                $lauf++;
            }
        }
    
        $html = '<option value="0">bitte wählen</option>'.$inkl.$more;
    
        return $html;
    }
    
    
    add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select_shuttleprice' );
    function wpcf7_add_form_tag_select_shuttleprice() {
        wpcf7_add_form_tag(
            array(
                'select_shuttleprice',
                'select_shuttleprice*',
            ),
            'wpcf7_select_shuttleprice_form_tag_handler',
            array(
                'name-attr'         => true,
                'selectable-values' => true,
            )
        );
    }
    
    function wpcf7_select_shuttleprice_form_tag_handler( $tag ) {
        return str_replace( '</select>', shuttleprice() . '</select>', str_replace(
            '<option value="">---</option>', '', wpcf7_select_form_tag_handler( $tag )
        ) );
    }
    
    
    add_filter( 'wpcf7_validate_select_shuttleprice', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
    add_filter( 'wpcf7_validate_select_shuttleprice*', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
    function wpcf7_select_shuttleprice_validation_filter( $result, $tag ) {
        $name = $tag->name;
        $empty = ( empty( $_POST[ $name ] ) || '0' === $_POST[ $name ] );
    
        if ( $tag->is_required() && $empty ) {
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
        }
    
        return $result;
    }
    

    有了这个简码

    [select_shuttleprice* shuttleprice-1 class:shuttleprice]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-10
      • 2019-03-28
      • 2020-07-18
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 2021-10-02
      • 2019-02-02
      相关资源
      最近更新 更多