重力形式字段上的最小字符

时间:2013-12-31 19:07:46

标签: forms validation character gravity minimum

真的想在场上制作最小字符 - 试过这个:

// Added custom validation for minimum word count
add_filter("gform_field_validation_11_70", "validate_word_count", 10, 4);

function validate_word_count($result, $value, $form, $field){
if (str_word_count($value) < 150) //Minimum number of words
{
    $result["is_valid"] = false;
    $result["message"] = "Please enter at least 150 words.";
}
return $result;
}

但我正在寻找一种验证字符而非单词的方法......

3 个答案:

答案 0 :(得分:1)

您的解决方案看起来非常好 - 谢谢!,但是: 结束使用以下内容:

    // Added custom validation for minimum characters count
    add_filter("gform_field_validation_3_8", "validate_chars_count", 10, 4);
    add_filter("gform_field_validation_1_5", "validate_chars_count", 10, 4);
    function validate_chars_count($result, $value, $form, $field){
if (strlen($value) < 9) { //Minimum number of characters
    $result["is_valid"] = false;
    $result["message"] = "Please enter at least 9 numbers.";
}
return $result;
    }

由于我正在尝试进行电话号码验证,我想限制用户仅填写数字或“ - ”这可以通过Gravity Forms实现吗?

答案 1 :(得分:0)

替换“str_word_count()”函数,只需使用“strlen()”。

另外,我已将此作为代码段编写,以便您可以更轻松地对不同字段应用不同的限制:

https://gist.github.com/spivurno/8220561

将代码段复制并粘贴到主题的 functions.php 文件中,然后修改表单底部的配置。您可以通过复制和粘贴此配置并修改所需表单/字段的参数,将其应用于不同的表单和字段。

new GW_Minimum_Characters( array(
    'form_id' => 385,
    'field_id' => 1,
    'min_chars' => 4,
    'validation_message' => __( 'Oops! You need to enter at least %s characters.' )
) );

答案 2 :(得分:0)

我遇到了同样的问题,我需要最小编号。文本字段和文本区域的字符集。此设置必须以不同的形式和多个字段使用,因此我无法添加具有预定义的表单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;
}