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

Custom validation message for select field in Contact form 7 WordPress

我想显示 select 字段的自定义消息。我正在为自定义消息使用插件 "Contact form 7 Custom validation",但它不适用于 select 字段。是否有一个钩子可以只修改 select 字段的消息,因为我的验证消息的其余部分没问题。

Update:

我有以下字段:

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

但这不起作用。

试试下面的代码:

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状态id:statefirst_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"]

已测试并有效。

// 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]