forked from kartik-v/yii2-password
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordInput.php
108 lines (98 loc) · 3.16 KB
/
PasswordInput.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* @package yii2-password
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016
* @version 1.5.3
*/
namespace kartik\password;
use kartik\base\InputWidget;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* PasswordInput widget is a wrapper for the JQuery Strength meter plugin by Krajee. The plugin converts a password
* input into a widget with an advanced strength validation meter and toggle mask to show/hide the password. The
* password strength is validated as you type.
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
* @see http://plugins.krajee.com/strength-meter
*/
class PasswordInput extends InputWidget
{
/**
* @var string the password strength meter language. If not provided or no translation is available, this will
* default to `en` (US English).
*/
public $language;
/**
* @var string the password input size. Defaults to medium size ('md'). Can be set 'lg' for large size or 'sm' for
* small size.
*/
public $size = 'md';
/**
* @var string the toggle mask placement with respect to the password input. Should be 'left' or 'right'. Defaults
* to 'right'.
*/
public $togglePlacement = 'right';
/**
* @inheritdoc
*/
public $pluginName = 'strength';
/**
* @inheritdoc
*/
public function run()
{
$this->initLanguage();
if ($this->hasModel()) {
$this->name = ArrayHelper::remove(
$this->options,
'name',
Html::getInputName($this->model, $this->attribute)
);
$this->value = $this->model[$this->attribute];
}
echo $this->getInput('passwordInput');
if (empty($this->pluginOptions['inputTemplate']) &&
($this->size === 'lg' || $this->size === 'sm' || $this->togglePlacement === 'left')
) {
$this->pluginOptions['inputTemplate'] = $this->renderInputTemplate();
}
$this->registerAssets();
parent::run();
}
/**
* Renders the input template
*
* @return string
*/
protected function renderInputTemplate()
{
$class = 'input-group';
$content = '{input}<span class="input-group-addon">{toggle}</span>';
if ($this->size === 'lg' || $this->size === 'sm') {
$class .= ' input-group-' . $this->size;
}
if ($this->togglePlacement === 'left') {
$content = '<span class="input-group-addon">{toggle}</span>{input}';
}
return "<div class='{$class}'>{$content}</div>";
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
$path = Yii::getAlias("@vendor/kartik-v/strength-meter");
$this->setLanguage('strength-meter-', $path, 'js/locales');
if (!empty($this->_langFile)) {
PasswordInputAsset::register($view)->js[] = $this->_langFile;
} else {
PasswordInputAsset::register($view);
}
$this->registerPlugin($this->pluginName);
}
}