Skip to content

Commit 4f83701

Browse files
committed
gw-quantity-as-decimal.php: Fixed an issue with Calculation Product field not editable with decimal values.
1 parent 0d1c480 commit 4f83701

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

gravity-forms/gw-quantity-as-decimal.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ function init() {
4343
add_filter( 'gform_field_validation_' . $this->form_id, array( $this, 'allow_quantity_float' ), 10, 4 );
4444
}
4545

46-
if ( GFFormsModel::is_html5_enabled() ) {
47-
add_filter( 'gform_pre_render', array( $this, 'stash_current_form' ) );
48-
add_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
46+
// For GF versions before 2.8 and HTML5 disabled, ignore the rest.
47+
if ( version_compare( GFCommon::$version, '2.8', '<' ) && ! GFFormsModel::is_html5_enabled() ) {
48+
return;
4949
}
5050

51+
// For GF versions 2.8 and beyond, HTML5 is enabled by default.
52+
// Also for GF versions prior to 2.8 having HTML5 manually enabled.
53+
add_filter( 'gform_pre_render', array( $this, 'stash_current_form' ) );
54+
add_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
55+
5156
add_filter( 'gform_field_content', array( $this, 'fix_content' ), 10, 5 );
5257
}
5358

@@ -89,8 +94,23 @@ function modify_quantity_input_tag( $markup, $field, $value, $lead_id, $form_id
8994
}
9095

9196
function fix_content( $content, $field, $value, $lead_id, $form_id ) {
92-
// ensure the quantity min attribute.
93-
return preg_replace( '/\smin=["\']0["\']/', 'min="0" step="any"', $content );
97+
// ensure the step is 'any' for any fields that have a decimal value.
98+
return preg_replace_callback(
99+
'/<input([^>]*class=[\'"]ginput_quantity[\'"][^>]*)>/i',
100+
function ( $matches ) {
101+
$inputTag = $matches[0];
102+
103+
// Check if the input has a decimal value, and if does not have 'any'.
104+
if ( preg_match('/\bvalue=[\'"]([\d]+\.[\d]+)[\'"]/i', $inputTag, $valueMatch ) ) {
105+
if ( ! preg_match('/\bstep=[\'"]any[\'"]/i', $inputTag ) ) {
106+
$inputTag = preg_replace( '/<input/i', '<input step="any"', $inputTag, 1 );
107+
}
108+
}
109+
110+
return $inputTag;
111+
},
112+
$content
113+
);
94114
}
95115

96116
function get_field_input( $field, $value, $form ) {

0 commit comments

Comments
 (0)