| 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 |
|
| 75 |
return $errors; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* IE fallback for number fields |
| 80 |
* Remove the comma when HTML5 isn't supported |
| 81 |
* |
| 82 |
* @since 3.0 |
| 83 |
*/ |
| 84 |
private function remove_commas_from_number( &$args ) { |
| 85 |
if ( strpos( $args['value'], ',' ) ) { |
| 86 |
$args['value'] = str_replace( ',', '', $args['value'] ); |
| 87 |
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Force the value to be numeric before it's saved in the DB |
| 93 |
*/ |
| 94 |
public function set_value_before_save( $value ) { |
| 95 |
if ( ! is_numeric( $value ) ) { |
| 96 |
$value = (float) $value; |
| 97 |
} |
| 98 |
|
| 99 |
return $value; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @since 4.0.04 |
| 104 |
*/ |
| 105 |
public function sanitize_value( &$value ) { |
| 106 |
FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
| 107 |
} |
| 108 |
} |
| 109 |
|