是否可以为我们拥有的每个表单限制输入字段中的字符? 我需要一个带有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”。
也许有一种方法可以为输入字段使用参数名称?!
答案 0 :(得分: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;
}
您可以将设置从最小长度更改为确切长度,然后相应地更新验证。
希望这会有所帮助。