【问题标题】:Wordpress - Contact Form 7 number field step and required attributeWordpress - 联系表格 7 数字字段步骤和必填属性
【发布时间】:2019-04-13 16:00:46
【问题描述】:

我在自定义表格 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。

【问题讨论】:

  • 问题是,联系表格 7 中的数字类型不接受浮点(十进制)作为 step 属性的值。 Int(整数)在标签生成代码中被硬编码。这就是为什么当您将步长值设置为“0.05”时,它不会在数字字段的代码中打印步长属性。

标签: html wordpress


【解决方案1】:

您可以通过自定义来实现这一点。在您的主题或子主题的 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 );

上面的代码所做的是,广告一个自定义表单标签“numberstepfloat”,它实际上输出输入类型数字,但包含 step 属性。自定义标签的代码广告验证中的另一个功能。验证仅检查该值是否存在。如果不是,则使该字段无效。

与Contact Form 7中的原生标签类似,自定义标签的使用方式如下。

[numberstepfloat* number-491 min:0 max:100000 step:0.05 id:abc placeholder "CMP*"]

希望这会有所帮助。

【讨论】:

  • 谢谢@zipkundan。我将您的代码添加到我的主题的 functions.php 文件中。它非常适用于numberstepfloat*(必需),我以 0.05 或我指定的任何增量获得步骤,但是当我使用numberstepfloat 时,我的字段被调整大小。因此,对于非必填字段,我已恢复为 &lt;input type="number" name="CMP" placeholder="CMP*" min=0 max=100000 step="0.05" &gt; 非常感谢您的帮助。
  • 您可以通过 css 调整字段的大小。这应该是可以管理的。联系表格 7 css 规则与字段类型相关。比如,input[type="number"] 等。但是您可以将自定义类应用于您的“numberstepfloat”类型字段并为它们添加自定义 css 规则。顺便说一句,既然答案解决了你的主要问题,你能把它标记为接受吗?
  • 我在 css 中尝试过,'#abc{ step: 0.05 }' 但是我收到错误“Unknown property 'step'。您的代码帮助解决了我的问题。我不知道我需要将其标记为已接受,现在就完成了。非常感谢。
  • 'step" 不是正确的 css 属性。如果您能告诉我您想如何设计该字段的样式,我会尽力帮助您处理正确的 css。
  • 非常感谢@zipkundan 跟进此事。我能够使用您的代码和我的问题中的 html 代码解决我的问题。我是初学者,需要更多帮助。如果您能帮助我解决其他问题,我将不胜感激。到目前为止,假设我有两个字段和一些单选按钮,如果其中一个字段中的值大于另一个字段,我希望能够防止按下提交按钮。
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2017-02-15
  • 1970-01-01
相关资源
最近更新 更多