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