Skip to content

Commit df9429b

Browse files
Update Integer and Float Inputfields to support min/max ranges. Also remove unnecessary "<p>" tags surrounded many of the Inputfields.
1 parent 4ed2edd commit df9429b

9 files changed

+114
-36
lines changed

wire/modules/Inputfield/InputfieldCheckboxes/InputfieldCheckboxes.css

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
margin: 0 !important;
66
}
77

8-
.Inputfields .InputfieldCheckboxes input {
9-
/* a little padding to the right of a checkbox/radio so label isn't up against the input */
10-
margin-right: 0.25em;
11-
}
12-
138
.InputfieldCheckboxes table {
149
width: 100%;
1510
}

wire/modules/Inputfield/InputfieldEmail.module

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class InputfieldEmail extends InputfieldText {
1616

1717
public function __construct() {
1818
parent::__construct();
19-
$this->setAttribute('type', 'text');
19+
$this->setAttribute('type', 'email');
2020
$this->setAttribute('maxlength', 512);
2121
//$this->setAttribute('size', 70);
2222
$this->setAttribute('name', 'email');

wire/modules/Inputfield/InputfieldFloat.module

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class InputfieldFloat extends InputfieldInteger {
66
return array(
77
'title' => __('Float', __FILE__), // Module Title
88
'summary' => __('Floating point number with precision', __FILE__), // Module Summary
9-
'version' => 100,
9+
'version' => 101,
1010
'permanent' => true,
1111
);
1212
}
@@ -16,8 +16,34 @@ class InputfieldFloat extends InputfieldInteger {
1616
parent::__construct();
1717
}
1818

19+
public function init() {
20+
parent::init();
21+
$this->attr('step', 'any'); // HTML5 attr required to support decimals with 'number' types
22+
}
23+
1924
protected function sanitizeValue($value) {
2025
return strlen("$value") ? round((float) $value, $this->precision) : '';
2126
}
2227

28+
/**
29+
* Returns true if number is in valid range, false if not
30+
*
31+
* Overriding the function from InputfieldInteger to ensure float types (rather than int types) are used
32+
*
33+
*/
34+
protected function isInRange($value) {
35+
$inRange = true;
36+
$min = $this->attr('min');
37+
$max = $this->attr('max');
38+
if(strlen("$value") && ($min || $max)) {
39+
if(strlen("$min") && ((float) $value) < ((float) $min)) {
40+
$inRange = false;
41+
}
42+
if(strlen("$max") && ((float) $value) > ((float) $max)) {
43+
$inRange = false;
44+
}
45+
}
46+
return $inRange;
47+
}
48+
2349
}

wire/modules/Inputfield/InputfieldInteger.module

+80-18
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,49 @@ class InputfieldInteger extends Inputfield {
66
return array(
77
'title' => __('Integer', __FILE__), // Module Title
88
'summary' => __('Integer (positive or negative)', __FILE__), // Module Summary
9-
'version' => 100,
9+
'version' => 101,
1010
'permanent' => true,
1111
);
1212
}
1313

1414
public function init() {
1515
parent::init();
1616
$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
1919
$this->attr('size', '10');
2020
}
2121

2222
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;
2441
return $out;
2542
}
2643

2744
protected function sanitizeValue($value) {
2845
$value = trim($value);
2946
if(!strlen("$value")) return '';
3047
$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.
3250
if(!strlen("$value")) return '';
51+
if(strpos($value, '.') !== false || strpos($value, ',') !== false) $value = round($value);
3352
$value = (int) $value;
3453
if($negative) $value = -1 * $value;
3554
return $value;
@@ -39,28 +58,71 @@ class InputfieldInteger extends Inputfield {
3958
return strlen("{$this->value}") === 0;
4059
}
4160

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+
4276
public function setAttribute($key, $value) {
4377

4478
if($key == 'value') {
45-
4679
$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+
}
4784

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+
}
5387

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;
6092
}
93+
return parent::set($key, $value);
94+
}
6195

96+
public function getConfigInputfields() {
97+
$inputfields = parent::getConfigInputfields();
6298

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;
64126
}
65127

66128
}

wire/modules/Inputfield/InputfieldRadios/InputfieldRadios.css

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
margin: 0 !important;
66
}
77

8-
.Inputfields .InputfieldRadios input {
9-
/* a little padding to the right of a checkbox/radio so label isn't up against the input */
10-
margin-right: 0.25em;
11-
}
12-
138
.InputfieldRadiosColumns,
149
.InputfieldRadiosFloated {
1510
width: 100%;

wire/modules/Inputfield/InputfieldRadios/InputfieldRadios.module

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class InputfieldRadios extends InputfieldSelect {
5454
"type='radio' " .
5555
"name='{$this->name}' " .
5656
"id='$id' " .
57-
"value='" . htmlspecialchars($key, ENT_QUOTES) . "' />" . $this->entityEncode($value) .
57+
"value='" . htmlspecialchars($key, ENT_QUOTES) . "' /> " . $this->entityEncode($value) .
5858
"</label></li>";
5959
}
6060

wire/modules/Inputfield/InputfieldSelect.module

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ class InputfieldSelect extends Inputfield {
235235
$attrs = $this->getAttributes();
236236
unset($attrs['value']);
237237

238-
$out = "\n<p><select " . $this->getAttributesString($attrs) . ">" .
238+
$out = "\n<select " . $this->getAttributesString($attrs) . ">" .
239239
$this->renderOptions($this->options) .
240-
"\n</select></p>";
240+
"\n</select>";
241241

242242
return $out;
243243
}

wire/modules/Inputfield/InputfieldText.module

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class InputfieldText extends Inputfield {
2727
}
2828

2929
public function ___render() {
30-
$out = "\n<p><input " . $this->getAttributesString() . " /></p>";
30+
$out = "\n<input " . $this->getAttributesString() . " />";
3131
return $out;
3232
}
3333

wire/modules/Inputfield/InputfieldTextarea.module

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class InputfieldTextarea extends Inputfield {
2828
$attrs = $this->getAttributes();
2929
unset($attrs['value']);
3030

31-
$out = "\n<p><textarea " . $this->getAttributesString($attrs) . ">" .
31+
$out = "\n<textarea " . $this->getAttributesString($attrs) . ">" .
3232
htmlspecialchars($this->value) .
33-
"</textarea></p>";
33+
"</textarea>";
3434
return $out;
3535
}
3636

0 commit comments

Comments
 (0)