联系表单7 WordPress中选择字段的自定义验证消息

问题描述 投票:0回答:2

我想显示选择字段的自定义消息。我正在使用插件“联系表单7自定义验证”来获取自定义消息,但它不适用于选择字段。是否有一个钩子只能为select字段修改我的消息,因为我的验证消息的其余部分没问题。

更新:

我有以下字段:

<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
2个回答
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/

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


0
投票

测试和工作。

// 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]
© www.soinside.com 2019 - 2024. All rights reserved.