Wordpress - 联系表格7 - 验证自定义字段

时间:2018-02-14 14:27:30

标签: php wordpress contact-form-7

我需要验证联系表格7中的自定义选择字段。

联系表格7中的自定义代码[mycode]生成以下HTML:

<select name="shuttleprice-1" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required shuttleprice" aria-required="true" aria-invalid="false">
    <option value="0">please choose</option>
    <option value="1">for 1 Person</option>
    <option value="2">for 2 Persons</option>
</select>

官方文档中有一篇关于自定义验证的帖子:https://contactform7.com/2015/03/28/custom-validation/

我把它作为我的function.php中的构建

add_filter( 'wpcf7_validate_select', 'custom_shuttleprice_validation_filter', 20, 2 );
function custom_shuttleprice_validation_filter( $result, $tag ) {
    if ( $tag->name == 'shuttleprice-1' ) {
        if( $_POST['shuttleprice-1'] == 0 ) {
            $result->invalidate( $tag, "Fix input" );
        }
    }
    return $result;
}

没有错误,但我仍然可以在不更改选择的情况下发送表单。

我有什么问题吗? “wpcf7_validate_select”是问题吗?

编辑: 这里[mycode]的代码(在我的代码中称为[shuttleprice])函数:

// For the custom Price for shuttle transport
function shuttleprice($atts) {

    $formname = $atts["name"];      
    $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 = '<select name="'.$formname.'" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required shuttleprice" aria-required="true" aria-invalid="false">
                <option value="0">bitte wählen</option>'.$inkl.$more.'</select>';

    return $html;
}
add_shortcode('shuttleprice', 'shuttleprice');
add_filter( 'wpcf7_form_elements', 'shuttle1_wpcf7_form_elements' );

function shuttle1_wpcf7_form_elements( $form ) {
    $form = do_shortcode( $form );
    return $form;
}

根据条件建立一个选择并不奇怪。

1 个答案:

答案 0 :(得分:1)

[编辑]新答案; 经过测试和运作

替换它:

add_filter( 'wpcf7_validate_select', 'custom_shuttleprice_validation_filter', 20, 2 );
function custom_shuttleprice_validation_filter( $result, $tag ) {
    if ( $tag->name == 'shuttleprice-1' ) {
        if( $_POST['shuttleprice-1'] == 0 ) {
            $result->invalidate( $tag, "Fix input" );
        }
    }
    return $result;
}

..还有这个:

// For the custom Price for shuttle transport
function shuttleprice($atts) {

    // To make this message shorter, I removed the code that was here.
}
add_shortcode('shuttleprice', 'shuttleprice');
add_filter( 'wpcf7_form_elements', 'shuttle1_wpcf7_form_elements' );

function shuttle1_wpcf7_form_elements( $form ) {
    $form = do_shortcode( $form );
    return $form;
}

..用这一个:

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

并将[mycode]替换为:

[select_shuttleprice* shuttleprice-1 class:shuttleprice]
相关问题