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

106 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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 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.04
101 */
102 public function sanitize_value( &$value ) {
103 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
104 }
105 }
106