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