【问题标题】:Gravity Forms: Require minimum/maximum characters in input field重力形式:要求输入字段中的最小/最大字符
【发布时间】:2018-02-10 17:13:59
【问题描述】:

是否可以为我们拥有的每个表单限制输入字段中的字符? 我需要一个包含 5 个数字的输入字段(zip 字段)。

我找到了这个解决方案: (完整代码:https://gravitywiz.com/require-minimum-character-limit-gravity-forms/

new GW_Minimum_Characters( array( 
    'form_id' => 524,
    'field_id' => 1,
    'min_chars' => 4,
    'max_chars' => 5,
    'min_validation_message' => __( 'Oops! You need to enter at least %s characters.' ),
    'max_validation_message' => __( 'Oops! You can only enter %s characters.')
    ) 
);

问题是,我们有几十个表单,无法为所有表单构建一个函数;) 所以我们不能使用“form_id”和“field_id”。

也许有一种方法可以为输入字段使用参数名称?

【问题讨论】:

    标签: wordpress gravity-forms-plugin gravityforms


    【解决方案1】:

    我有一个类似的问题,我需要最低数量。文本字段和文本区域的字符数。此设置必须用于不同的表单和多个字段,因此我无法添加具有预定义表单 ID 和字段 ID 的过滤器。

    我所做的是创建了一个在编辑字段时可见的新设置,然后我验证了提交。您可以使用以下代码:

    add_action( 'gform_field_standard_settings', 'minimum_field_setting', 10 );
    add_action( 'gform_editor_js', 'editor_script' );
    add_filter( 'gform_tooltips', 'add_encryption_tooltips' );
    add_filter( 'gform_validation', 'general_validation' );
    
    /**
     * Adds the Minimum Characters Field to Form Fields
     *
     * @param integer $position
     */
    function minimum_field_setting( $position ) {
    
        //Position: Underneath Description TextArea
        if ( $position == 75 ) {
            ?>
            <li class="minlen_setting field_setting">
                <label for="field_minlen" class="section_label">
                    <?php esc_html_e( 'Minimum Characters', 'gravityforms' ); ?>
                    <?php gform_tooltip( 'form_field_minlen' ) ?>
                </label>
                <input type="number"
                       id="field_minlen"
                       onblur="SetFieldProperty('minLength', this.value);"
                       value=""
                />
            </li>
            <?php
        }
    }
    
    /**
     * Adds Javascript to Gravity Forms in order to render the new setting field in their appropriate field types
     */
    function editor_script() {
        ?>
        <script type='text/javascript'>
            //Append field setting only to text and textarea fields
            jQuery.each(fieldSettings, function (index, value) {
                if (index === 'textarea' || index === 'text') {
                    fieldSettings[index] += ", .minlen_setting";
                }
            });
            //binding to the load field settings event to initialize the checkbox
            jQuery(document).bind("gform_load_field_settings", function (event, field, form) {
                if (field.type === 'textarea' || field.type === 'text') {
                    console.log(field);
                    if (typeof field.minLength !== "undefined") {
                        jQuery("#field_minlen").attr("value", field.minLength);
                    } else {
                        jQuery("#field_minlen").attr("value", '');
                    }
                }
            });
        </script>
        <?php
    }
    
    /**
     * Add GF Tooltip for Minimum Length
     *
     * @param array $tooltips
     *
     * @return mixed
     */
    function add_encryption_tooltips( $tooltips ) {
        $tooltips['form_field_minlen'] = "<h6>Minimum Length</h6>Minimum number of characters for this field";
    
        return $tooltips;
    }
    
    /**
     * Validate Form Submission
     *
     * @param array $validation_result
     *
     * @return mixed
     */
    function general_validation( $validation_result ) {
        $form = $validation_result['form'];
    
        foreach ( $form['fields'] as &$field ) {
            if ( in_array( $field->type, [ 'text', 'textarea' ] ) && ! empty( $field->minLength ) ) {
                $input_name = 'input_' . $field->id;
                if ( isset( $_POST[ $input_name ] ) && $_POST[ $input_name ] != '' ) {
                    if ( strlen( $_POST[ $input_name ] ) < (int) $field->minLength ) {
                        $field->failed_validation      = true;
                        $field->validation_message     = 'Field must contain at least ' . $field->minLength . ' characters';
                        $validation_result['is_valid'] = false;
                    }
                }
            }
        }
        $validation_result['form'] = $form;
    
        return $validation_result;
    }
    

    您可以将设置从 Minimum Length 更改为 Exact Length,然后相应地更新验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 2020-06-07
      • 2017-12-25
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      相关资源
      最近更新 更多