Wordpress - 联系表 7 - 验证自定义字段
Wordpress - Contact Form 7 - Validate Custom Field
我需要验证联系表 7 中的自定义 select 字段。
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>
官方文档有一个关于自定义验证的post: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;
}
没有错误,但我仍然可以在不更改 select 的情况下发送表格。
我做错了什么吗?
"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;
}
它只是根据条件构建 select 而已。
[编辑] 新答案; 已测试并有效。
替换为:
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 字段。
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>
官方文档有一个关于自定义验证的post: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;
}
没有错误,但我仍然可以在不更改 select 的情况下发送表格。
我做错了什么吗? "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;
}
它只是根据条件构建 select 而已。
[编辑] 新答案; 已测试并有效。
替换为:
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]