PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.22.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.22.2
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
formidable / classes / models / fields / FrmFieldNumber.php

FrmFieldNumber.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.22.2, at classes/models/fields/FrmFieldNumber.php

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