[英]Wordpress - Contact Form 7 - Validate Custom Field
I need to validate a custom select field in Contact Form 7.我需要验证联系表 7 中的自定义选择字段。
The custom code [mycode] in the Contact Form 7 is generating the following HTML:联系表 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>
The official documentation has a post about custom validation: https://contactform7.com/2015/03/28/custom-validation/官方文档有一篇关于自定义验证的帖子: https : //contactform7.com/2015/03/28/custom-validation/
I took this an build it in my function.php我把它建在我的 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;
}
There are no errors but i still can send the form without changing the select.没有错误,但我仍然可以在不更改选择的情况下发送表单。
Is there something i am doing wrong?有什么我做错了吗? Is the "wpcf7_validate_select" the Problem?
“wpcf7_validate_select”是问题吗?
EDIT: Here the code of the [mycode] (called [shuttleprice] in my code) function:编辑:这里是 [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;
}
It does nothing fancy just building a select based on conditions.它只是根据条件构建一个选择,没什么特别的。
[EDIT] New answer; [编辑] 新答案; tested and works .
测试和工作。
Replace this:替换这个:
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;
}
.. and also this: ..还有这个:
// 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;
}
.. with this one: .. 用这个:
// 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;
}
And replace the [mycode]
with:并将
[mycode]
替换为:
[select_shuttleprice* shuttleprice-1 class:shuttleprice]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.