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