Php Wordpress-联系表单7-验证自定义字段
我需要验证联系人表单7中的自定义选择字段 联系人表单7中的自定义代码[mycode]正在生成以下HTML:Php Wordpress-联系表单7-验证自定义字段,php,wordpress,contact-form-7,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 c
<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>
没有错误,但我仍然可以发送表单,而无需更改选择
我做错什么了吗?
“wpcf7\u验证\u选择”是否是问题所在
编辑:
这里是[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;
}
//有关穿梭运输的自定义价格
功能羽毛球价格($atts){
$formname=$atts[“name”];
$max_personen=get_字段(“maximale_anzahl”,$id_a);
$max_personen_gesamt=get_field(“anzahl_maximale_personen_im_shuttle_mit_aufpreis”,$id_a);
$aufpreis=get_字段(“aufpreis_pro_person_im_shuttle”,$id_a);
$inkl=“”;
$more=“”;
对于($x=1;$x[编辑]新答案;已测试且有效
替换此项:
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;
}
尝试if(空($\u POST['shuttleprice-1'])){
?尝试挂接wpcf7\u validate\u mycode
。抱歉,这两条评论都没有改变任何内容。验证筛选器无法工作,因为联系人表单7系统未添加select
元素。我已更新了我的答案,请查看并让我知道。感谢您的回复!select似乎是注册的现在在CF7中编辑,您的代码很好,但是当我发送表单时,我收到一个错误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 dispatch@jquery.js?ver=1.12.4:3 r.handle@jquery.js.js?ver=1.12.7:3哪个版本您使用的是rsion吗?另外,是否有专门为“shuttleprice”编写的JS/jQuery脚本选择
/下拉菜单?尝试禁用AJAX提交一段时间,看看表单是否正确提交。可以帮助您禁用AJAX提交。我使用的是CF7的4.7。应用程序需要on_Send_ok功能,以便我可以更新到4.9,但我认为不会有太多不同。禁用AJAX后,出现新错误:无法从新插件的control.js中读取属性'contactFormId'of undefined'。并且自定义字段仍然没有标记为丢失。因此,我刚刚尝试了一些方法,现在表单正在发送。但是,在他离开控制台的站点之前,出现了与之前相同的POST错误,然后他重定向到一个空页面(与表单相同的URL,但未加载)。没有任何错误消息。我认为当前已实现的JS可能存在一些问题。。
// 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]