|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @since 3.0 |
|
5
|
|
|
*/ |
|
6
|
|
|
class FrmFieldNumber extends FrmFieldType { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var string |
|
10
|
|
|
* @since 3.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $type = 'number'; |
|
13
|
|
|
protected $display_type = 'text'; |
|
14
|
|
|
|
|
15
|
|
|
protected function field_settings_for_type() { |
|
16
|
|
|
$settings = array( |
|
17
|
|
|
'size' => true, |
|
18
|
|
|
'clear_on_focus' => true, |
|
19
|
|
|
'invalid' => true, |
|
20
|
|
|
'range' => true, |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
$frm_settings = FrmAppHelper::get_settings(); |
|
24
|
|
|
if ( $frm_settings->use_html ) { |
|
25
|
|
|
$settings['max'] = false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $settings; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function extra_field_opts() { |
|
32
|
|
|
return array( |
|
33
|
|
|
'minnum' => 0, |
|
34
|
|
|
'maxnum' => 9999999, |
|
35
|
|
|
'step' => 'any', |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @since 3.01.03 |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function add_extra_html_atts( $args, &$input_html ) { |
|
43
|
|
|
$this->add_min_max( $args, $input_html ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function validate( $args ) { |
|
47
|
|
|
$errors = array(); |
|
48
|
|
|
|
|
49
|
|
|
$this->remove_commas_from_number( $args ); |
|
50
|
|
|
|
|
51
|
|
|
//validate the number format |
|
52
|
|
View Code Duplication |
if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) { |
|
|
|
|
|
|
53
|
|
|
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $errors; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* IE fallback for number fields |
|
77
|
|
|
* Remove the comma when HTML5 isn't supported |
|
78
|
|
|
* |
|
79
|
|
|
* @since 3.0 |
|
80
|
|
|
*/ |
|
81
|
|
|
private function remove_commas_from_number( &$args ) { |
|
82
|
|
|
if ( strpos( $args['value'], ',' ) ) { |
|
83
|
|
|
$args['value'] = str_replace( ',', '', $args['value'] ); |
|
84
|
|
|
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Force the value to be numeric before it's saved in the DB |
|
90
|
|
|
*/ |
|
91
|
|
|
public function set_value_before_save( $value ) { |
|
92
|
|
|
if ( ! is_numeric( $value ) ) { |
|
93
|
|
|
$value = (float) $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $value; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @since 4.0 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function sanitize_value( &$value ) { |
|
103
|
|
|
FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.