我在自定义表单7数字字段中添加必需和步骤属性时有问题。我试过用两种不同的方法,用同样的形式,有两个不同的问题。
我正在尝试添加一个所需的数字字段和一个0.05的步长。
1) [number* number-491 id:abc min:0 max:100000 step:0.05 placeholder “CMP*”] -这是尊重步骤大小属性,而不是步骤:0.05
2) <input type=”number” name=“CMP” placeholder=“CMP*” min=0 max=100000 step=“0.05” required/> --这尊重步骤大小属性,而不是必需的属性。
我可以使用任何一种或另一种方法,我想要的只是一个数字字段,它将是一个必需的字段,步骤大小为0.05。
发布于 2018-11-11 22:51:37
您可以通过定制来实现这一点。在主题或子主题的functions.php文件中使用以下代码。
function wpcf7_number_floating_step_form_tag_handler( $tag ) {
if ( empty( $tag->name ) ) {
return '';
}
if( $tag->type == 'numberstepfloat*' ){
$tag->type = 'number*';
$tag->basetype = 'number';
}
$validation_error = wpcf7_get_validation_error( $tag->name );
$class = wpcf7_form_controls_class( $tag->type );
$class .= ' wpcf7-validates-as-number-decimal';
if ( $validation_error ) {
$class .= ' wpcf7-not-valid';
}
$atts = array();
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
$atts['min'] = $tag->get_option( 'min', 'signed_int', true );
$atts['max'] = $tag->get_option( 'max', 'signed_int', true );
$atts['step'] = $tag->get_option( 'step', '', true );
if ( $tag->has_option( 'readonly' ) ) {
$atts['readonly'] = 'readonly';
}
if ( $tag->is_required() ) {
$atts['aria-required'] = 'true';
}
$atts['aria-invalid'] = $validation_error ? 'true' : 'false';
$value = (string) reset( $tag->values );
if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) {
$atts['placeholder'] = $value;
$value = '';
}
$value = $tag->get_default_option( $value );
$value = wpcf7_get_hangover( $tag->name, $value );
$atts['value'] = $value;
if ( wpcf7_support_html5() ) {
$atts['type'] = $tag->basetype;
} else {
$atts['type'] = 'text';
}
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts( $atts );
$html = sprintf(
'<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
sanitize_html_class( $tag->name ), $atts, $validation_error );
return $html;
}
function custom_add_form_tag_floatingnumber() {
wpcf7_add_form_tag( array( 'numberstepfloat', 'numberstepfloat*' ), 'wpcf7_number_floating_step_form_tag_handler', array( 'name-attr' => true ) );
}
add_action( 'wpcf7_init', 'custom_add_form_tag_floatingnumber' );
function custom_numberstepfloat_validation($result, $tag){
$customnumfield = $tag->name;
if($_POST[$customnumfield]=='' || $_POST[$customnumfield]==null){
$result->invalidate( $tag, "This field is required." );
}
return $result;
}
add_filter( 'wpcf7_validate_numberstepfloat*', 'custom_numberstepfloat_validation', 20, 2 );上面的代码所做的是,广告一个自定义的表单标签“数字逐步浮动”,它实际上输出输入类型编号,但包含step属性。代码中的另一个函数对自定义标记进行验证。验证只检查值是否存在。如果没有,则使字段无效。
与联系人表单7中的本机标记类似,自定义标记将按以下方式使用。
[numberstepfloat* number-491 min:0 max:100000 step:0.05 id:abc placeholder "CMP*"]希望这能有所帮助。
https://stackoverflow.com/questions/53236635
复制相似问题