-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntlTelNumberValidator.php
77 lines (62 loc) · 1.78 KB
/
IntlTelNumberValidator.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
<?php
namespace indicalabs\phone;
use \Yii;
use yii\helpers\Json;
use yii\validators\Validator;
use yii\validators\ValidationAsset;
class IntlTelNumberValidator extends Validator
{
public function init()
{
parent::init();
if (!$this->message) {
$this->message = Yii::t('app', '{attribute} is not a valid number.');
}
}
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if (!true) {
$model->addError($attribute, $this->message);
}
}
public function clientValidateAttribute($model, $attribute, $view)
{
$className = explode('\\', $model->className());
$className = strtolower(end($className));
$formName = strtolower($model->formName());
$label = $model->getAttributeLabel($attribute);
$options = [
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $label,
], Yii::$app->language),
];
$formName = strtolower($model->formName());
$options = Json::htmlEncode($options);
ValidationAsset::register($view);
$view->registerJs(<<<JS
(function(){
var telInput = $("#$formName-$attribute");
if (!telInput.intlTelInput('isValidNumber')) {
messages.push($options.message);
}
var intlNumber = telInput.intlTelInput("getNumber");
telInput.value = intlNumber;
document.getElementById("$className-$attribute").value = intlNumber;
document.getElementById("$className-$attribute").disabled = false;
//var countryData = $.fn.intlTelInput.getCountryData();
//console.log(countryData);
})();
JS
, \yii\web\View::POS_LOAD);
// $view->registerJs(<<<JS
// (function(){
// var telInput = $('#$formName-$attribute');
// if (!telInput.intlTelInput('isValidNumber')) {
// messages.push($options.message);
// }
// })();
// JS
// , \yii\web\View::POS_LOAD);
}
}