|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
use yii\helpers\Json; |
|
12
|
|
|
use yii\web\JsExpression; |
|
13
|
|
|
use yii\web\View; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* MaskedInput generates a masked text input. |
|
17
|
|
|
* |
|
18
|
|
|
* MaskedInput is similar to [[Html::textInput()]] except that an input mask will be used to force users to enter |
|
19
|
|
|
* properly formatted data, such as phone numbers, social security numbers. |
|
20
|
|
|
* |
|
21
|
|
|
* To use MaskedInput, you must set the [[mask]] property. The following example |
|
22
|
|
|
* shows how to use MaskedInput to collect phone numbers: |
|
23
|
|
|
* |
|
24
|
|
|
* ```php |
|
25
|
|
|
* echo MaskedInput::widget([ |
|
26
|
|
|
* 'name' => 'phone', |
|
27
|
|
|
* 'mask' => '999-999-9999', |
|
28
|
|
|
* ]); |
|
29
|
|
|
* ``` |
|
30
|
|
|
* |
|
31
|
|
|
* You can also use this widget in an [[ActiveForm]] using the [[ActiveField::widget()|widget()]] |
|
32
|
|
|
* method, for example like this: |
|
33
|
|
|
* |
|
34
|
|
|
* ```php |
|
35
|
|
|
* <?= $form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::class, [ |
|
36
|
|
|
* 'mask' => '999-999-9999', |
|
37
|
|
|
* ]) ?> |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* The masked text field is implemented based on the |
|
41
|
|
|
* [jQuery input masked plugin](https://github.com/RobinHerbots/Inputmask). |
|
42
|
|
|
* |
|
43
|
|
|
* @author Kartik Visweswaran <[email protected]> |
|
44
|
|
|
* @since 2.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
class MaskedInput extends InputWidget |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* The name of the jQuery plugin to use for this widget. |
|
50
|
|
|
*/ |
|
51
|
|
|
const PLUGIN_NAME = 'inputmask'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string|array|JsExpression the input mask (e.g. '99/99/9999' for date input). The following characters |
|
55
|
|
|
* can be used in the mask and are predefined: |
|
56
|
|
|
* |
|
57
|
|
|
* - `a`: represents an alpha character (A-Z, a-z) |
|
58
|
|
|
* - `9`: represents a numeric character (0-9) |
|
59
|
|
|
* - `*`: represents an alphanumeric character (A-Z, a-z, 0-9) |
|
60
|
|
|
* - `[` and `]`: anything entered between the square brackets is considered optional user input. This is |
|
61
|
|
|
* based on the `optionalmarker` setting in [[clientOptions]]. |
|
62
|
|
|
* |
|
63
|
|
|
* Additional definitions can be set through the [[definitions]] property. |
|
64
|
|
|
*/ |
|
65
|
|
|
public $mask; |
|
66
|
|
|
/** |
|
67
|
|
|
* @var array custom mask definitions to use. Should be configured as `maskSymbol => settings`, where |
|
68
|
|
|
* |
|
69
|
|
|
* - `maskSymbol` is a string, containing a character to identify your mask definition and |
|
70
|
|
|
* - `settings` is an array, consisting of the following entries: |
|
71
|
|
|
* - `validator`: string, a JS regular expression or a JS function. |
|
72
|
|
|
* - `cardinality`: int, specifies how many characters are represented and validated for the definition. |
|
73
|
|
|
* - `prevalidator`: array, validate the characters before the definition cardinality is reached. |
|
74
|
|
|
* - `definitionSymbol`: string, allows shifting values from other definitions, with this `definitionSymbol`. |
|
75
|
|
|
*/ |
|
76
|
|
|
public $definitions; |
|
77
|
|
|
/** |
|
78
|
|
|
* @var array custom aliases to use. Should be configured as `maskAlias => settings`, where |
|
79
|
|
|
* |
|
80
|
|
|
* - `maskAlias` is a string containing a text to identify your mask alias definition (e.g. 'phone') and |
|
81
|
|
|
* - `settings` is an array containing settings for the mask symbol, exactly similar to parameters as passed in [[clientOptions]]. |
|
82
|
|
|
*/ |
|
83
|
|
|
public $aliases; |
|
84
|
|
|
/** |
|
85
|
|
|
* @var array the JQuery plugin options for the input mask plugin. |
|
86
|
|
|
* @see https://github.com/RobinHerbots/Inputmask |
|
87
|
|
|
*/ |
|
88
|
|
|
public $clientOptions = []; |
|
89
|
|
|
/** |
|
90
|
|
|
* @var array the HTML attributes for the input tag. |
|
91
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
92
|
|
|
*/ |
|
93
|
|
|
public $options = ['class' => 'form-control']; |
|
94
|
|
|
/** |
|
95
|
|
|
* @var string the type of the input tag. Currently only 'text' and 'tel' are supported. |
|
96
|
|
|
* @see https://github.com/RobinHerbots/Inputmask |
|
97
|
|
|
* @since 2.0.6 |
|
98
|
|
|
*/ |
|
99
|
|
|
public $type = 'text'; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var string the hashed variable to store the pluginOptions |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $_hashVar; |
|
105
|
|
|
/** |
|
106
|
|
|
* @var string[] the inputmask properties can be contained callbacks |
|
107
|
|
|
*/ |
|
108
|
|
|
protected $_jsCallbacks = [ |
|
109
|
|
|
'oncomplete', |
|
110
|
|
|
'onincomplete', |
|
111
|
|
|
'oncleared', |
|
112
|
|
|
'onKeyDown', |
|
113
|
|
|
'onBeforeMask', |
|
114
|
|
|
'onBeforePaste', |
|
115
|
|
|
'onBeforeWrite', |
|
116
|
|
|
'onUnMask', |
|
117
|
|
|
'onKeyValidation', |
|
118
|
|
|
'isComplete', |
|
119
|
|
|
// @deprecated removed in 5.0: |
|
120
|
|
|
'preValidation', |
|
121
|
|
|
'postValidation', |
|
122
|
|
|
// @deprecated removed in 4.0: |
|
123
|
|
|
'canClearPosition' |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Initializes the widget. |
|
128
|
|
|
* |
|
129
|
|
|
* @throws InvalidConfigException if the "mask" property is not set. |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function init() |
|
132
|
|
|
{ |
|
133
|
1 |
|
parent::init(); |
|
134
|
1 |
|
if (empty($this->mask) && empty($this->clientOptions['alias'])) { |
|
135
|
|
|
throw new InvalidConfigException("Either the 'mask' property or the 'clientOptions[\"alias\"]' property must be set."); |
|
136
|
|
|
} |
|
137
|
1 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function run() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->registerClientScript(); |
|
145
|
|
|
echo $this->renderInputHtml($this->type); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Generates a hashed variable to store the plugin `clientOptions`. |
|
150
|
|
|
* |
|
151
|
|
|
* Helps in reusing the variable for similar |
|
152
|
|
|
* options passed for other widgets on the same page. The following special data attribute will also be |
|
153
|
|
|
* added to the input field to allow accessing the client options via javascript: |
|
154
|
|
|
* |
|
155
|
|
|
* - 'data-plugin-inputmask' will store the hashed variable storing the plugin options. |
|
156
|
|
|
* |
|
157
|
|
|
* @param View $view the view instance |
|
158
|
|
|
* @author [Thiago Talma](https://github.com/thiagotalma) |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function hashPluginOptions($view) |
|
161
|
|
|
{ |
|
162
|
|
|
$encOptions = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions); |
|
163
|
|
|
$this->_hashVar = self::PLUGIN_NAME . '_' . hash('crc32', $encOptions); |
|
164
|
|
|
$this->options['data-plugin-' . self::PLUGIN_NAME] = $this->_hashVar; |
|
165
|
|
|
$view->registerJs("var {$this->_hashVar} = {$encOptions};", View::POS_HEAD); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Initializes client options. |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function initClientOptions() |
|
172
|
|
|
{ |
|
173
|
|
|
$options = $this->clientOptions; |
|
174
|
|
|
foreach ($options as $key => $value) { |
|
175
|
|
|
if ( |
|
176
|
|
|
!empty($value) |
|
177
|
|
|
&& !$value instanceof JsExpression |
|
178
|
|
|
&& in_array($key, $this->_jsCallbacks, true) |
|
179
|
|
|
) { |
|
180
|
|
|
$options[$key] = new JsExpression($value); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
$this->clientOptions = $options; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Registers the needed client script and options. |
|
188
|
|
|
*/ |
|
189
|
|
|
public function registerClientScript() |
|
190
|
|
|
{ |
|
191
|
|
|
$js = ''; |
|
192
|
|
|
$view = $this->getView(); |
|
193
|
|
|
$this->initClientOptions(); |
|
194
|
|
|
if (!empty($this->mask)) { |
|
195
|
|
|
$this->clientOptions['mask'] = $this->mask; |
|
196
|
|
|
} |
|
197
|
|
|
$this->hashPluginOptions($view); |
|
198
|
|
|
if (!empty($this->definitions) && is_array($this->definitions)) { |
|
199
|
|
|
$js .= ucfirst(self::PLUGIN_NAME) . '.extendDefinitions(' . Json::htmlEncode($this->definitions) . ');'; |
|
200
|
|
|
} |
|
201
|
|
|
if (!empty($this->aliases) && is_array($this->aliases)) { |
|
202
|
|
|
$js .= ucfirst(self::PLUGIN_NAME) . '.extendAliases(' . Json::htmlEncode($this->aliases) . ');'; |
|
203
|
|
|
} |
|
204
|
|
|
$id = $this->options['id']; |
|
205
|
|
|
$js .= 'jQuery("#' . $id . '").' . self::PLUGIN_NAME . '(' . $this->_hashVar . ');'; |
|
206
|
|
|
MaskedInputAsset::register($view); |
|
207
|
|
|
$view->registerJs($js); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|