1 <?php
2 3 4 5 6 7 8 9
10 class RangeField extends FormField {
11
12 protected $minField = null;
13 protected $maxField = null;
14
15 protected $minValue = null;
16 protected $maxValue = null;
17
18 protected $unit = null;
19
20 function __construct($name, $title = null, $value = "", $className = 'NumericField', $MinMaxValue = false){
21 $this->minField = new $className($name . '[min]', false);
22 $this->maxField = new $className($name . '[max]', false);
23
24 if ($MinMaxValue && isset($MinMaxValue['min']) && isset($MinMaxValue['max'])) {
25 $this->minValue = $MinMaxValue['min'];
26 $this->maxValue = $MinMaxValue['max'];
27 }
28 parent::__construct($name, $title, $value);
29 }
30
31 function setForm($form) {
32 parent::setForm($form);
33
34 $this->minField->setForm($form);
35 $this->maxField->setForm($form);
36 }
37
38 function Field() {
39 return '<label class="min" MinValue="' . $this->minValue . '">' . _t('RangeField.FROM', 'from') . $this->minField->Field() . '</label> <label class="max" MaxValue="' . $this->maxValue . '">' . _t('RangeField.TO', 'to') . $this->maxField->Field() . '</label>' . ($this->unit ? '<span class="form_rangeUnit">'.$this->unit.'</span>' : '');
40 }
41
42 function setValue($val) {
43 if(empty($val)) {
44 $this->minField->setValue(null);
45 $this->maxField->setValue(null);
46 } else {
47 if(is_string($val) && strpos($val, ',') !== false) {
48 list($min, $max) = explode(',', $val);
49 $this->minField->setValue($min);
50 $this->maxField->setValue($max);
51 }
52 elseif(is_array($val)) {
53 if (array_key_exists('min', $val)) {
54 $this->minField->setValue($val['min']);
55 }
56 if (array_key_exists('max', $val)) {
57 $this->maxField->setValue($val['max']);
58 }
59 } else {
60 $this->minField->setValue($val);
61 $this->maxField->setValue($val);
62 }
63 }
64 }
65
66 function setMinMaxValue($MinMaxValue) {
67 if ($MinMaxValue && isset($MinMaxValue['min']) && isset($MinMaxValue['max'])) {
68 $this->minValue = $MinMaxValue['min'];
69 $this->maxValue = $MinMaxValue['max'];
70 }
71 }
72
73 function setUnit($unit) {
74 if ($unit) {
75 $this->addExtraAttribute('Unit', trim($unit));
76 }
77 else {
78 $this->removeExtraAttribute('Unit');
79 }
80 $this->unit = $unit;
81 }
82
83 function getUnit() {
84 return $this->unit;
85 }
86
87 function dataValue() {
88 $min = $this->minField->dataValue();
89 $max = $this->maxField->dataValue();
90
91 if($min && $max) {
92 return array(
93 'min' => $min,
94 'max' => $max,
95 );
96 return $min . ',' . $max;
97 }
98 return null;
99 }
100
101 102 103
104 function getMinField() {
105 return $this->minField;
106 }
107
108 109 110
111 function getMaxField() {
112 return $this->maxField;
113 }
114
115 function validate($validator) {
116 return ($this->minField->validate($validator) && $this->maxField->validate($validator));
117 }
118
119 function jsValidation() {
120 return $this->minField->jsValidation() . $this->maxField->jsValidation();
121 }
122
123 function performReadonlyTransformation() {
124 return $this->minField->performReadonlyTransformation() . $this->maxField->performReadonlyTransformation();
125 }
126 }
127
128
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-