Html Wordpress-联系人表单7编号字段步骤和必填属性
在自定义表单7编号字段中添加必需属性和步骤属性时遇到问题。我试过以同样的形式使用两种不同的方法,但遇到了两个不同的问题 我正在尝试添加一个必需的数字字段和0.05的步长 1[编号*编号-491 id:abc最小值:0最大值:100000步数:0.05占位符“CMP*”]–这将尊重步数属性,但不尊重步数:0.05 2–这尊重步长属性,但不是必需的属性Html Wordpress-联系人表单7编号字段步骤和必填属性,html,wordpress,Html,Wordpress,在自定义表单7编号字段中添加必需属性和步骤属性时遇到问题。我试过以同样的形式使用两种不同的方法,但遇到了两个不同的问题 我正在尝试添加一个必需的数字字段和0.05的步长 1[编号*编号-491 id:abc最小值:0最大值:100000步数:0.05占位符“CMP*”]–这将尊重步数属性,但不尊重步数:0.05 2–这尊重步长属性,但不是必需的属性 我可以使用其中一种或另一种方法,我只需要一个数字字段,它是必填字段,步长为0.05 您可以通过定制来实现这一点。在主题或子主题的functions.
我可以使用其中一种或另一种方法,我只需要一个数字字段,它是必填字段,步长为0.05 您可以通过定制来实现这一点。在主题或子主题的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 );
希望这有帮助。问题在于,联系人表单7中的数字类型不接受浮点小数作为步长属性的值。Int integer在标记生成代码中硬编码。这就是为什么当您将步长值设置为0.05时,它不会在编码字段中打印步长属性的原因。谢谢@zipkundan。我将您的代码添加到my theme的functions.php文件中。它适用于numberstepfloat*required,我以0.05或我指定的任何增量获得步骤,但是,当我使用numberstepfloat时,我的字段的大小被调整了。因此,对于非必填字段,我再次感谢您的帮助。您可以通过css调整字段的大小。这应该是可控的。联系人表单7 css规则与文件类型绑定。例如,输入[type=number]等,但您可以将自定义类应用于numberstepfloat类型字段,并为它们添加自定义css规则。顺便说一句,既然答案解决了您的主要问题,您能将其标记为已接受吗?我在css中尝试了“abc{step:0.05}”,但是我得到了一个错误未知属性“step”。您的代码帮助解决了我的问题。我没有意识到我需要将其标记为已接受,是吗。非常感谢。步骤不是正确的css属性。如果你能让我知道你想如何风格化这个领域,我会尝试帮助你正确的css。非常感谢@zipkundan的跟进。我能够使用您的代码和问题中的html代码解决我的问题。我是初学者,需要更多的帮助。如果您能在其他问题上帮助我,我将不胜感激。现在,假设我有两个字段和一些单选按钮,如果其中一个字段的值大于另一个字段的值,我希望能够防止按下submit按钮。
[numberstepfloat* number-491 min:0 max:100000 step:0.05 id:abc placeholder "CMP*"]