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