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