【问题标题】:Wordpress - Contact Form 7 - Validate Custom FieldWordpress - 联系表格 7 - 验证自定义字段
【发布时间】:2018-02-14 14:27:29
【问题描述】:

我需要验证 Contact Form 7 中的自定义选择字段。

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

它只是根据条件构建一个选择。

【问题讨论】:

  • 试试if( empty($_POST['shuttleprice-1']) ) { ?
  • 尝试勾搭wpcf7_validate_mycode
  • 抱歉,两个 cmets 都没有改变任何东西..

标签: php wordpress contact-form-7


【解决方案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]

【讨论】:

  • 验证过滤器不起作用,因为联系表单 7 系统未添加 select 元素。我已经更新了我的答案,所以请查看并告诉我。
  • 感谢您的回复! select 现在似乎已在 CF7 中注册,并且您的代码很好,但是当我发送表单时,我收到 POST Error 500 send @ jquery.js?ver=1.12.4:4 ajax @ jquery.js?ver= 1.12.4:4 e.fn.ajaxSubmit@jquery.form.min.js?v….51.0-2014.06.20:11 t@jquery.form.min.js?v….51.0-2014.06.20:11 调度@jquery.js?ver=1.12.4:3 r.handle @jquery.js?ver=1.12.4:3
  • 您使用的是哪个 CF7 版本?另外,是否有专门为“shuttleprice”select/下拉菜单编写的 JS/jQuery 脚本?尝试禁用 AJAX 提交一段时间,看看表单是否正确提交。 This plugin可以帮你禁用AJAX提交。
  • 我使用的是 4.7 的 CF7。该应用程序需要 on_sent_ok 功能,因此我可以更新到 4.9,但我认为不会有很多不同。禁用 AJAX 后,新插件的 control.js 出现新错误:“无法读取未定义的属性 'contactFormId'”。并且自定义字段仍然没有标记为缺失。
  • 所以,我只是尝试了一些东西,现在表格正在发送。然而,在他离开站点之前,在控制台中出现与以前相同的 POST 错误,然后他重定向到一个空页面(与表单相同的 URL,但它没有加载)。没有任何错误消息..我认为当前已经实现的 JS 可能存在一些问题..
猜你喜欢
  • 2014-05-16
  • 2019-08-30
  • 2020-07-18
  • 2019-03-28
  • 1970-01-01
  • 2021-10-02
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多