PluginProbe
Hash Form – Drag & Drop Form Builder / trunk
Hash Form – Drag & Drop Form Builder vtrunk
1.4.4 1.4.3 1.4.2 1.4.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.6.1 1.2.7 1.2.8 1.2.9 1.3.0 All 47 releases
hash-form / includes / fields / HashFormFieldNumber.php

HashFormFieldNumber.php in Hash Form – Drag & Drop Form Builder trunk, at includes/fields/HashFormFieldNumber.php

93 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || die();
3
4 class HashFormFieldNumber extends HashFormFieldType {
5
6 protected $type = 'number';
7
8 protected function field_settings_for_type() {
9 return array(
10 'advanced_validation' => true,
11 'clear_on_focus' => true,
12 'invalid' => true,
13 'range' => true,
14 );
15 }
16
17 protected function extra_field_default_opts() {
18 return array(
19 'minnum' => 0,
20 'maxnum' => 9999999,
21 'step' => 1
22 );
23 }
24
25 public function validate($args) {
26 $errors = array();
27 $this->remove_commas_from_number($args);
28
29 if (!is_numeric($args['value']) && '' !== $args['value'])
30 // $args is the parameter, not a property: $this->args does not
31 // exist, so the form title resolved from null on every failure.
32 $errors['field' . $args['id']] = apply_filters('hashform_translate_string', HashFormFields::get_error_msg($this->field, 'invalid'), 'Hash Form', HashFormBuilder::get_form_title(isset($args['form_id']) ? $args['form_id'] : 0) . ' - ' . $args['id'] . ' - ' . 'Field Validation Message');
33
34 if ($args['value'] != '') {
35 $minnum = HashFormFields::get_option($this->field, 'minnum');
36 $maxnum = HashFormFields::get_option($this->field, 'maxnum');
37 if ($maxnum !== '' && $minnum !== '') {
38 $value = (float) $args['value'];
39 if ($value < $minnum) {
40 $errors['field' . $args['id']] = esc_html__('Please select a higher number', 'hash-form');
41 } elseif ($value > $maxnum) {
42 $errors['field' . $args['id']] = esc_html__('Please select a lower number', 'hash-form');
43 }
44 }
45 $this->validate_step($errors, $args);
46 }
47 return $errors;
48 }
49
50 private function validate_step(&$errors, $args) {
51 if (isset($errors['field' . $args['id']]))
52 return;
53 $step = HashFormFields::get_option($this->field, 'step');
54 if (!$step || !is_numeric($step))
55 return;
56 $result = $this->check_value_is_valid_with_step($args['value'], $step);
57 if (!$result)
58 return;
59 /* translators: 1: nearest number, 2: nearest number */
60 $errors['field' . $args['id']] = sprintf(esc_html__('Please enter a valid value. Two nearest valid values are %1$s and %2$s', 'hash-form'), floatval($result[0]), floatval($result[1]));
61 }
62
63 private function check_value_is_valid_with_step($value, $step) {
64 $decimals = max(HashFormHelper::count_decimals($value), HashFormHelper::count_decimals($step));
65 $pow = pow(10, $decimals);
66 $value = intval($pow * $value);
67 $step = intval($pow * $step);
68 $div = $value / $step;
69 if (is_int($div))
70 return 0;
71 $div = floor($div);
72 return array($div * $step / $pow, ($div + 1) * $step / $pow);
73 }
74
75 private function remove_commas_from_number(&$args) {
76 if (strpos($args['value'], ',')) {
77 $args['value'] = str_replace(',', '', $args['value']);
78 }
79 }
80
81 public function sanitize_value(&$value) {
82 return hashform_sanitize_float($value);
83 }
84
85 protected function input_html() {
86 $field_type = $this->type;
87 ?>
88 <input type="<?php echo esc_attr($field_type); ?>" <?php $this->field_attrs(); ?> />
89 <?php
90 }
91
92 }
93