【发布时间】: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