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