| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.0 |
| 8 |
*/ |
| 9 |
class FrmFieldNumber extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
* @since 3.0 |
| 14 |
*/ |
| 15 |
protected $type = 'number'; |
| 16 |
protected $display_type = 'text'; |
| 17 |
|
| 18 |
protected function field_settings_for_type() { |
| 19 |
$settings = array( |
| 20 |
'size' => true, |
| 21 |
'clear_on_focus' => true, |
| 22 |
'invalid' => true, |
| 23 |
'range' => true, |
| 24 |
); |
| 25 |
|
| 26 |
$frm_settings = FrmAppHelper::get_settings(); |
| 27 |
if ( $frm_settings->use_html ) { |
| 28 |
$settings['max'] = false; |
| 29 |
} |
| 30 |
|
| 31 |
return $settings; |
| 32 |
} |
| 33 |
|
| 34 |
protected function extra_field_opts() { |
| 35 |
return array( |
| 36 |
'minnum' => 0, |
| 37 |
'maxnum' => 9999999, |
| 38 |
'step' => 'any', |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @since 3.01.03 |
| 44 |
*/ |
| 45 |
protected function add_extra_html_atts( $args, &$input_html ) { |
| 46 |
$this->add_min_max( $args, $input_html ); |
| 47 |
} |
| 48 |
|
| 49 |
public function validate( $args ) { |
| 50 |
$errors = array(); |
| 51 |
|
| 52 |
$this->remove_commas_from_number( $args ); |
| 53 |
|
| 54 |
//validate the number format |
| 55 |
if ( ! is_numeric( $args['value'] ) && '' !== $args['value'] ) { |
| 56 |
$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' ); |
| 57 |
} |
| 58 |
|
| 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 |
} |
| 73 |
|
| 74 |
$this->validate_step( $errors, $args ); |
| 75 |
} |
| 76 |
|
| 77 |
return $errors; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Validates the step setting. |
| 82 |
* |
| 83 |
* @since 5.2.06 |
| 84 |
* |
| 85 |
* @param array $errors Errors array. |
| 86 |
* @param array $args Validation args. |
| 87 |
*/ |
| 88 |
private function validate_step( &$errors, $args ) { |
| 89 |
if ( isset( $errors[ 'field' . $args['id'] ] ) ) { |
| 90 |
return; // Don't need to check if value is invalid before. |
| 91 |
} |
| 92 |
|
| 93 |
$step = FrmField::get_option( $this->field, 'step' ); |
| 94 |
if ( ! $step || ! is_numeric( $step ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$value = (float) $args['value']; |
| 99 |
$step = (float) $step; |
| 100 |
$div = $value / $step; |
| 101 |
if ( floor( $div ) === $div ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$div = floor( $div ); |
| 106 |
$errors[ 'field' . $args['id'] ] = sprintf( |
| 107 |
// Translators: %1$s: the first nearest value; %2$s: the second nearest value. |
| 108 |
__( 'Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'formidable' ), |
| 109 |
floatval( $div * $step ), |
| 110 |
floatval( ( $div + 1 ) * $step ) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* IE fallback for number fields |
| 116 |
* Remove the comma when HTML5 isn't supported |
| 117 |
* |
| 118 |
* @since 3.0 |
| 119 |
*/ |
| 120 |
private function remove_commas_from_number( &$args ) { |
| 121 |
if ( strpos( $args['value'], ',' ) ) { |
| 122 |
$args['value'] = str_replace( ',', '', $args['value'] ); |
| 123 |
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Force the value to be numeric before it's saved in the DB |
| 129 |
*/ |
| 130 |
public function set_value_before_save( $value ) { |
| 131 |
if ( ! is_numeric( $value ) ) { |
| 132 |
$value = (float) $value; |
| 133 |
} |
| 134 |
|
| 135 |
return $value; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @since 4.0.04 |
| 140 |
*/ |
| 141 |
public function sanitize_value( &$value ) { |
| 142 |
FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
| 143 |
} |
| 144 |
} |
| 145 |
|