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