1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\FormElements; |
4
|
|
|
|
5
|
|
|
use SilverStripe\i18n\i18n; |
6
|
|
|
use SilverStripe\Forms\TextField; |
7
|
|
|
use SilverStripe\View\Requirements; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Format input using input mask |
11
|
|
|
* |
12
|
|
|
* Fully decouples formatted field from data field. |
13
|
|
|
* Formatting is a UI concept that should not be dealt with in PHP outside of the scope of validation. |
14
|
|
|
* This avoids messy conversion (for date, currency, ...) |
15
|
|
|
*/ |
16
|
|
|
class InputMaskField extends TextField implements MaskableField, LocalizableField |
17
|
|
|
{ |
18
|
|
|
use BaseElement; |
19
|
|
|
use Localize; |
20
|
|
|
|
21
|
|
|
// Base masks |
22
|
|
|
const MASK_NUMERIC = '9'; |
23
|
|
|
const MASK_ALPHA = 'a'; |
24
|
|
|
const MASK_ALPHANUMERIC = '*'; |
25
|
|
|
// Base alias |
26
|
|
|
const ALIAS_URL = 'url'; |
27
|
|
|
const ALIAS_IP = 'ip'; |
28
|
|
|
const ALIAS_EMAIL = 'email'; |
29
|
|
|
const ALIAS_DATETIME = 'datetime'; |
30
|
|
|
const ALIAS_NUMERIC = 'numeric'; |
31
|
|
|
const ALIAS_CURRENCY = 'currency'; |
32
|
|
|
const ALIAS_DECIMAL = 'decimal'; |
33
|
|
|
const ALIAS_INTEGER = 'integer'; |
34
|
|
|
const ALIAS_PERCENTAGE = 'percentage'; |
35
|
|
|
const ALIAS_PHONE = 'phone'; |
36
|
|
|
const ALIAS_PHONEBE = 'phonebe'; |
37
|
|
|
const ALIAS_REGEX = 'regex'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @config |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $default_config = []; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Override locale. If empty will default to current locale |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $locale = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Format to use for storage |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private static $defaut_dataformat = null; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Format to use for storage |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $dataFormat; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @config |
68
|
|
|
* @var boolean |
69
|
|
|
*/ |
70
|
|
|
private static $enable_requirements = true; |
|
|
|
|
71
|
|
|
|
72
|
|
|
public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) |
73
|
|
|
{ |
74
|
|
|
parent::__construct($name, $title, $value, $maxLength, $form); |
75
|
|
|
$this->mergeDefaultConfig(); |
76
|
|
|
$this->dataFormat = self::config()->default_dataformat; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function Type() |
80
|
|
|
{ |
81
|
|
|
return 'inputmask'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function extraClass() |
85
|
|
|
{ |
86
|
|
|
return 'text ' . parent::extraClass(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getPlaceholder() |
90
|
|
|
{ |
91
|
|
|
return $this->getAttribute('placeholder'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setPlaceholder($value) |
95
|
|
|
{ |
96
|
|
|
return $this->setAttribute('placeholder', $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getDataFormat() |
100
|
|
|
{ |
101
|
|
|
return $this->getElementAttribute('data-dataformat'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The value you want when unmasking to hidden field |
106
|
|
|
* |
107
|
|
|
* @param string $value The alias or "masked" to get the masked value as is |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setDataFormat($dataformat) |
111
|
|
|
{ |
112
|
|
|
return $this->getElementAttribute('data-dataformat', $dataformat); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getAlias() |
116
|
|
|
{ |
117
|
|
|
return $this->getConfig('alias'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setAlias($value) |
121
|
|
|
{ |
122
|
|
|
return $this->setConfig('alias', $value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getRegex() |
126
|
|
|
{ |
127
|
|
|
return $this->getConfig('regex'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Use a regular expression as a mask |
132
|
|
|
* |
133
|
|
|
* @link https://github.com/RobinHerbots/Inputmask#regex |
134
|
|
|
* @param string $value |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setRegex($value) |
138
|
|
|
{ |
139
|
|
|
return $this->setConfig('regex', $value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getMask() |
143
|
|
|
{ |
144
|
|
|
return $this->getConfig('mask'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the mask |
149
|
|
|
* |
150
|
|
|
* 9: numeric, a: alphabetical, *: alphanumeric, (aaa): optional part |
151
|
|
|
* |
152
|
|
|
* @param string $value |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setMask($value) |
156
|
|
|
{ |
157
|
|
|
return $this->setConfig('mask', $value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getRightAlign() |
161
|
|
|
{ |
162
|
|
|
return $this->getConfig('rightAlign'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setRighAlign($value) |
166
|
|
|
{ |
167
|
|
|
return $this->setConfig('rightAlign', $value); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getGroupSeparator() |
171
|
|
|
{ |
172
|
|
|
return $this->getConfig('groupSeparator'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setGroupSeparator($value) |
176
|
|
|
{ |
177
|
|
|
return $this->setConfig('groupSeparator', $value); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getRadixPoint() |
181
|
|
|
{ |
182
|
|
|
return $this->getConfig('radixPoint'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setRadixPoint($value) |
186
|
|
|
{ |
187
|
|
|
return $this->setConfig('radixPoint', $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getDigits() |
191
|
|
|
{ |
192
|
|
|
return $this->getConfig('digits'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setDigits($value) |
196
|
|
|
{ |
197
|
|
|
return $this->setConfig('digits', $value); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getAttributes() |
201
|
|
|
{ |
202
|
|
|
$attributes = parent::getAttributes(); |
203
|
|
|
$attributes['lang'] = i18n::convert_rfc1766($this->getLocale()); |
204
|
|
|
return $attributes; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function Field($properties = array()) |
208
|
|
|
{ |
209
|
|
|
if ($this->readonly) { |
210
|
|
|
$this->setAttribute("disabled", true); |
211
|
|
|
} |
212
|
|
|
return $this->wrapInElement('input-mask', $properties); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function requirements() |
216
|
|
|
{ |
217
|
|
|
Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/input-mask.min.js"); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|