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

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