/
IntlTelWidget.php
84 lines (71 loc) · 2.15 KB
/
IntlTelWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace indicalabs\phone;
use yii\widgets\InputWidget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Class IntlTelWidget
* Usage:
* <?= $form->field($model, 'mobile_number')->widget(indicalabs\phone\IntlTelWidget::className(),[
* 'clientOptions' => [
* 'initialCountry' => 'in',
* ]
* ]); ?>
*
*
* @author Venu Narukulla <venu.narukulla@gmail.com>
* @link
* @date 09/12/2013
* @time 09:00am
* @see https://github.com/twitter/typeahead.js
*/
class IntlTelWidget extends InputWidget
{
public $options = [];
public $clientOptions = [];
public function init()
{
parent::init();
$id=$this->getId();
if (isset($this->clientOptions['id']))
$id = $this->clientOptions['id'];
else
$this->clientOptions['id']=$id;
$this->clientOptions = ArrayHelper::merge([
'class' => 'form-control',
'defaultCountry' => 'auto',
'numberType' => 'MOBILE', //FIXED_LINE
//'separateDialCode' => 'true',
'autoHideDialCode' => 'true',
'nationalMode' => false,
'dropdownContainer' => 'body',
'preferredCountries' => ['in', 'us','gb'],
//'responsiveDropdown' => true,
], $this->clientOptions);
$this->options = ArrayHelper::merge($this->options,$this->clientOptions);
}
public function run()
{
$this->registerPlugin('intlTelInput');
if ($this->hasModel()) {
return Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
return Html::textInput($this->name, $this->value, $this->options);
}
}
/**
* Registers MultiSelect Bootstrap plugin and the related events
*/
protected function registerPlugin()
{
$view = $this->getView();
IntlTelAsset::register($view);
$id = $this->options['id'];
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '';
$js = "jQuery('#".$id."').intlTelInput($options);";
$view->registerJs($js);
}
}