| @@ -64,24 +64,25 @@ | ||
| 64 | 64 | if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) { |
| 65 | 65 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - if ( $args['value'] === '' ) { | |
| 69 | - return $errors; | |
| 70 | - } | |
| 68 | + // validate number settings | |
| 69 | + if ( $args['value'] != '' ) { | |
| 70 | + // only check if options are available in settings | |
| 71 | + $minnum = FrmField::get_option( $this->field, 'minnum' ); | |
| 72 | + $maxnum = FrmField::get_option( $this->field, 'maxnum' ); | |
| 73 | + if ( $maxnum !== '' && $minnum !== '' ) { | |
| 74 | + $value = (float) $args['value']; | |
| 75 | + if ( $value < $minnum ) { | |
| 76 | + $errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' ); | |
| 77 | + } elseif ( $value > $maxnum ) { | |
| 78 | + $errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' ); | |
| 79 | + } | |
| 80 | + } | |
| 71 | 81 | |
| 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' ); | |
| 82 | + $this->validate_step( $errors, $args ); | |
| 80 | 83 | } |
| 81 | 84 | |
| 82 | - $this->validate_step( $errors, $args ); | |
| 83 | - | |
| 84 | 85 | return $errors; |
| 85 | 86 | } |
| 86 | 87 | |
| 87 | 88 | /** |
| @@ -93,9 +94,9 @@ | ||
| 93 | 94 | * @param array $args Validation args. |
| 94 | 95 | * |
| 95 | 96 | * @return void |
| 96 | 97 | */ |
| 97 | - protected function validate_step( &$errors, $args ) { | |
| 98 | + private function validate_step( &$errors, $args ) { | |
| 98 | 99 | if ( isset( $errors[ 'field' . $args['id'] ] ) ) { |
| 99 | 100 | // Don't need to check if value is invalid before. |
| 100 | 101 | return; |
| 101 | 102 | } |
| @@ -112,10 +113,10 @@ | ||
| 112 | 113 | |
| 113 | 114 | $errors[ 'field' . $args['id'] ] = sprintf( |
| 114 | 115 | // Translators: %1$s: the first nearest value; %2$s: the second nearest value. |
| 115 | 116 | __( '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] | |
| 117 | + floatval( $result[0] ), | |
| 118 | + floatval( $result[1] ) | |
| 118 | 119 | ); |
| 119 | 120 | } |
| 120 | 121 | |
| 121 | 122 | /** |
| @@ -126,14 +127,14 @@ | ||
| 126 | 127 | * @param numeric $value The value. |
| 127 | 128 | * @param numeric $step The step. |
| 128 | 129 | * @return array|int Return `0` if valid. Otherwise, return an array contains two nearest values. |
| 129 | 130 | */ |
| 130 | - protected function check_value_is_valid_with_step( $value, $step ) { | |
| 131 | + private function check_value_is_valid_with_step( $value, $step ) { | |
| 131 | 132 | // Count the number of decimals. |
| 132 | - $decimals = (int) max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) ); | |
| 133 | + $decimals = max( FrmAppHelper::count_decimals( $value ), FrmAppHelper::count_decimals( $step ) ); | |
| 133 | 134 | |
| 134 | 135 | // Convert value and step to int to prevent precision problem. |
| 135 | - $pow = 10 ** $decimals; | |
| 136 | + $pow = pow( 10, $decimals ); | |
| 136 | 137 | $value = intval( $pow * $value ); |
| 137 | 138 | $step = intval( $pow * $step ); |
| 138 | 139 | $div = $value / $step; |
| 139 | 140 | if ( is_int( $div ) ) { |