/
TextInput.php
79 lines (69 loc) · 2.39 KB
/
TextInput.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
<?php
namespace mgcode\textInput;
use yii\helpers\Html;
use yii\base\Model;
use yii\validators\StringValidator;
use yii\widgets\InputWidget;
class TextInput extends InputWidget
{
/** @inheritdoc */
public $options = ['class' => 'form-control'];
public function run()
{
$this->normalizeMaxLength();
$this->registerClientScript();
$input = $this->renderInput();
if(!isset($this->options['maxlength'])) {
return $input;
}
$value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
$taken = mb_strlen($value, 'UTF-8');
$left = $this->options['maxlength'] - $taken;
return '<div class="input-group" data-field="count-characters">'.$input.'<span class="input-group-addon" data-field="characters-remaining">'.$left.'</span></div>';
}
/**
* Registers JavaScript asset
*/
protected function registerClientScript()
{
TextInputAsset::register($this->getView());
}
/**
* Renders input
* @return string
*/
protected function renderInput()
{
if($this->hasModel()) {
return Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
return Html::textInput($this->name, $this->value, $this->options);
}
}
/**
* If the model attribute is validated by a string validator,
* the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]].
* @param Model $model the model object
* @param string $attribute the attribute name or expression.
*/
protected function normalizeMaxLength()
{
if(isset($this->options['maxlength']) && $this->options['maxlength'] !== true) {
return;
}
unset($this->options['maxlength']);
if(!$this->hasModel()) {
return;
}
$attrName = Html::getAttributeName($this->attribute);
foreach ($this->model->getActiveValidators($attrName) as $validator) {
if ($validator instanceof StringValidator && $validator->max !== null) {
if ($validator->when === null || call_user_func($validator->when, $this->model, $this->attribute)) {
$this->options['maxlength'] = $validator->max;
return;
}
}
}
return;
}
}