@@ -6,30 +6,49 @@ class InputfieldInteger extends Inputfield {
6
6
return array(
7
7
'title' => __('Integer', __FILE__), // Module Title
8
8
'summary' => __('Integer (positive or negative)', __FILE__), // Module Summary
9
- 'version' => 100 ,
9
+ 'version' => 101 ,
10
10
'permanent' => true,
11
11
);
12
12
}
13
13
14
14
public function init() {
15
15
parent::init();
16
16
$this->attr('type', 'text');
17
- // $this->set('maxValue ', PHP_INT_MAX );
18
- // $this->set('minValue ', 0 );
17
+ $this->attr('min ', '' ); // blank means not set
18
+ $this->attr('max ', '' ); // blank means not set
19
19
$this->attr('size', '10');
20
20
}
21
21
22
22
public function ___render() {
23
- $out = "<input " . $this->getAttributesString() . " />";
23
+ if(!$this->attr('type')) $this->attr('type', 'text');
24
+ $attrs = $this->getAttributes();
25
+ $note = '';
26
+
27
+ if(empty($attrs['min']) && empty($attrs['max'])) {
28
+ // if both min+max are 0, then consider them non-applicable
29
+ unset($attrs['min'], $attrs['max']);
30
+ } else {
31
+ // unset any that aren't applicable
32
+ if(strlen("$attrs[min]")) $note .= sprintf($this->_('Min: %d'), $attrs['min']);
33
+ else unset($attrs['min']);
34
+ if(strlen("$attrs[max]")) $note .= ($note ? ', ' : '') . sprintf($this->_('Max: %d'), $attrs['max']);
35
+ else unset($attrs['max']);
36
+ }
37
+
38
+ if($note) $note = " <span class='details'>$note</span>";
39
+
40
+ $out = "<input " . $this->getAttributesString() . " />"; // . $note;
24
41
return $out;
25
42
}
26
43
27
44
protected function sanitizeValue($value) {
28
45
$value = trim($value);
29
46
if(!strlen("$value")) return '';
30
47
$negative = substr($value, 0, 1) === '-';
31
- $value = preg_replace('/[^\d]/', '', $value); // remove non digits, like commas, etc.
48
+ if($negative) $value = substr($value, 1);
49
+ if(!ctype_digit("$value")) $value = preg_replace('/[^\d,.]/', '', $value); // remove non digits, like commas, etc.
32
50
if(!strlen("$value")) return '';
51
+ if(strpos($value, '.') !== false || strpos($value, ',') !== false) $value = round($value);
33
52
$value = (int) $value;
34
53
if($negative) $value = -1 * $value;
35
54
return $value;
@@ -39,28 +58,71 @@ class InputfieldInteger extends Inputfield {
39
58
return strlen("{$this->value}") === 0;
40
59
}
41
60
61
+ protected function isInRange($value) {
62
+ $inRange = true;
63
+ $min = $this->attr('min');
64
+ $max = $this->attr('max');
65
+ if(strlen("$value") && ($min || $max)) {
66
+ if(strlen("$min") && ((int) $value) < ((int) $min)) {
67
+ $inRange = false;
68
+ }
69
+ if(strlen("$max") && ((int) $value) > ((int) $max)) {
70
+ $inRange = false;
71
+ }
72
+ }
73
+ return $inRange;
74
+ }
75
+
42
76
public function setAttribute($key, $value) {
43
77
44
78
if($key == 'value') {
45
-
46
79
$value = $this->sanitizeValue($value);
80
+ if(strlen("$value") && !$this->isInRange($value)) {
81
+ $this->error(sprintf($this->_('Value is out of bounds (min=%1$s, max=%2$s'), $this->attr('min'), $this->attr('max')));
82
+ }
83
+ }
47
84
48
- /* TO ADD BACK LATER
49
- if(strlen("$value")) {
50
- if(!is_null($this->maxValue) && ((int)$value) > $this->maxValue) {
51
- $value = '';
52
- $this->error("Value supplied for field '{$this->name}' exceeded the maximum allowed value");
85
+ return parent::setAttribute($key, $value);
86
+ }
53
87
54
- } else if(!is_null($this->minValue) && ((int)$value) < $this->minValue) {
55
- $value = '';
56
- $this->error("Value supplied for field '{$this->name}' is less than the minimum allowed value");
57
- }
58
- }
59
- */
88
+ public function set($key, $value) {
89
+ if($key == 'inputType') {
90
+ $this->attr('type', $value);
91
+ return $this;
60
92
}
93
+ return parent::set($key, $value);
94
+ }
61
95
96
+ public function getConfigInputfields() {
97
+ $inputfields = parent::getConfigInputfields();
62
98
63
- return parent::setAttribute($key, $value);
99
+ $f = wire('modules')->get('InputfieldRadios');
100
+ $f->attr('name', 'inputType');
101
+ $f->label = $this->_('Numeric Input Type');
102
+ $f->addOption('text', $this->_('Text'));
103
+ $f->addOption('number', $this->_('Number (HTML5)'));
104
+ $f->attr('value', $this->attr('type'));
105
+ $f->collapsed = Inputfield::collapsedYes;
106
+ $f->description = $this->_('Choosing the "Number" type enables some additional client-side validation in browsers that support it.');
107
+ $inputfields->add($f);
108
+
109
+ $f = wire('modules')->get('InputfieldFloat');
110
+ $f->attr('name', 'min');
111
+ $f->attr('value', $this->attr('min'));
112
+ $f->label = $this->_('Minimum Value');
113
+ $f->description = $this->_('The minimum allowed value for this field. Leave blank to ignore.');
114
+ $f->columnWidth = 50;
115
+ $inputfields->add($f);
116
+
117
+ $f = wire('modules')->get('InputfieldFloat');
118
+ $f->attr('name', 'max');
119
+ $f->attr('value', $this->attr('max'));
120
+ $f->label = $this->_('Maximum Value');
121
+ $f->description = $this->_('The maximum allowed value for this field. Leave blank to ignore.');
122
+ $f->columnWidth = 50;
123
+ $inputfields->add($f);
124
+
125
+ return $inputfields;
64
126
}
65
127
66
128
}
0 commit comments