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

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