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