PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/fields/FrmFieldNumber.php +39 -44 6.316.8 View file →
@@ -9,14 +9,18 @@
9 9 class FrmFieldNumber extends FrmFieldType {
10 10
11 11 /**
12 12 * @var string
13 - *
14 13 * @since 3.0
15 14 */
16 15 protected $type = 'number';
17 16
18 17 /**
18 + * @var string
19 + */
20 + protected $display_type = 'text';
21 +
22 + /**
19 23 * @var bool
20 24 */
21 25 protected $array_allowed = false;
22 26
@@ -30,9 +34,12 @@
30 34 'invalid' => true,
31 35 'range' => true,
32 36 );
33 37
34 - $settings['max'] = false;
38 + $frm_settings = FrmAppHelper::get_settings();
39 + if ( $frm_settings->use_html ) {
40 + $settings['max'] = false;
41 + }
35 42
36 43 return $settings;
37 44 }
38 45
@@ -49,11 +56,8 @@
49 56
50 57 /**
51 58 * @since 3.01.03
52 59 *
53 - * @param array $args
54 - * @param string $input_html
55 - *
56 60 * @return void
57 61 */
58 62 protected function add_extra_html_atts( $args, &$input_html ) {
59 63 $this->add_min_max( $args, $input_html );
@@ -58,11 +62,8 @@
58 62 protected function add_extra_html_atts( $args, &$input_html ) {
59 63 $this->add_min_max( $args, $input_html );
60 64 }
61 65
62 - /**
63 - * @param array $args
64 - */
65 66 public function validate( $args ) {
66 67 $errors = array();
67 68
68 69 $this->remove_commas_from_number( $args );
@@ -71,24 +72,26 @@
71 72 if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) {
72 73 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' );
73 74 }
74 75
75 - if ( $args['value'] === '' ) {
76 - return $errors;
77 - }
76 + // validate number settings
77 + if ( $args['value'] != '' ) {
78 + $frm_settings = FrmAppHelper::get_settings();
79 + // only check if options are available in settings
80 + $minnum = FrmField::get_option( $this->field, 'minnum' );
81 + $maxnum = FrmField::get_option( $this->field, 'maxnum' );
82 + if ( $frm_settings->use_html && $maxnum !== '' && $minnum !== '' ) {
83 + $value = (float) $args['value'];
84 + if ( $value < $minnum ) {
85 + $errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' );
86 + } elseif ( $value > $maxnum ) {
87 + $errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' );
88 + }
89 + }
78 90
79 - $value = (float) $args['value'];
80 - $minnum = FrmField::get_option( $this->field, 'minnum' );
81 - $maxnum = FrmField::get_option( $this->field, 'maxnum' );
82 -
83 - if ( $minnum !== '' && $value < $minnum ) {
84 - $errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' );
85 - } elseif ( $maxnum !== '' && $value > $maxnum ) {
86 - $errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' );
91 + $this->validate_step( $errors, $args );
87 92 }
88 93
89 - $this->validate_step( $errors, $args );
90 -
91 94 return $errors;
92 95 }
93 96
94 97 /**
@@ -100,9 +103,9 @@
100 103 * @param array $args Validation args.
101 104 *
102 105 * @return void
103 106 */
104 - protected function validate_step( &$errors, $args ) {
107 + private function validate_step( &$errors, $args ) {
105 108 if ( isset( $errors[ 'field' . $args['id'] ] ) ) {
106 109 // Don't need to check if value is invalid before.
107 110 return;
108 111 }
@@ -107,15 +110,13 @@
107 110 return;
108 111 }
109 112
110 113 $step = FrmField::get_option( $this->field, 'step' );
111 -
112 114 if ( ! $step || ! is_numeric( $step ) ) {
113 115 return;
114 116 }
115 117
116 118 $result = $this->check_value_is_valid_with_step( $args['value'], $step );
117 -
118 119 if ( ! $result ) {
119 120 return;
120 121 }
121 122
@@ -121,10 +122,10 @@
121 122
122 123 $errors[ 'field' . $args['id'] ] = sprintf(
123 124 // Translators: %1$s: the first nearest value; %2$s: the second nearest value.
124 125 __( 'Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'formidable' ),
125 - is_numeric( $result[0] ) ? floatval( $result[0] ) : $result[0],
126 - is_numeric( $result[1] ) ? floatval( $result[1] ) : $result[1]
126 + floatval( $result[0] ),
127 + floatval( $result[1] )
127 128 );
128 129 }
129 130
130 131 /**
@@ -133,21 +134,19 @@
133 134 * @since 5.2.07
134 135 *
135 136 * @param numeric $value The value.
136 137 * @param numeric $step The step.
137 - *
138 - * @return array|int Return `0` if valid. Otherwise, return an array contains two nearest values.
138 + * @return int|array Return `0` if valid. Otherwise, return an array contains two nearest values.
139 139 */
140 - protected function check_value_is_valid_with_step( $value, $step ) {
140 + private function check_value_is_valid_with_step( $value, $step ) {
141 141 // Count the number of decimals.
142 - $decimals = (int) max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) );
142 + $decimals = max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) );
143 143
144 144 // Convert value and step to int to prevent precision problem.
145 - $pow = 10 ** $decimals;
145 + $pow = pow( 10, $decimals );
146 146 $value = intval( $pow * $value );
147 147 $step = intval( $pow * $step );
148 148 $div = $value / $step;
149 -
150 149 if ( is_int( $div ) ) {
151 150 return 0;
152 151 }
153 152
@@ -165,31 +164,27 @@
165 164 *
166 165 * @return void
167 166 */
168 167 private function remove_commas_from_number( &$args ) {
169 - if ( ! str_contains( $args['value'], ',' ) ) {
170 - return;
168 + if ( strpos( $args['value'], ',' ) ) {
169 + $args['value'] = str_replace( ',', '', $args['value'] );
170 + FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
171 171 }
172 -
173 - $args['value'] = str_replace( ',', '', $args['value'] );
174 - FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
175 172 }
176 173
177 174 /**
178 175 * Force the value to be numeric before it's saved in the DB
179 - *
180 - * @param array|string $value
181 - *
182 - * @return float
183 176 */
184 177 public function set_value_before_save( $value ) {
185 - return is_numeric( $value ) ? $value : (float) $value;
178 + if ( ! is_numeric( $value ) ) {
179 + $value = (float) $value;
180 + }
181 +
182 + return $value;
186 183 }
187 184
188 185 /**
189 186 * @since 4.0.04
190 - *
191 - * @param array|string $value
192 187 *
193 188 * @return void
194 189 */
195 190 public function sanitize_value( &$value ) {