/
ActiveField.php
182 lines (154 loc) · 5.76 KB
/
ActiveField.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* ActiveField.php
* @author Revin Roman http://phptime.ru
*/
namespace common\widgets\angular;
use yii\helpers\Html;
/**
* Class ActiveField
* @package common\widgets\angular
*/
class ActiveField extends \yii\widgets\ActiveField
{
public $errorOptions = ['class' => 'error-balloon'];
/**
* @inheritdoc
*/
public function render($content = null)
{
if ($content === null) {
if (!isset($this->parts['{input}'])) {
$this->parts['{input}'] = Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
}
if (!isset($this->parts['{label}'])) {
$this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $this->labelOptions);
}
if (!isset($this->parts['{error}'])) {
$this->parts['{error}'] = $this->_error($this->model, $this->attribute, $this->errorOptions);
}
if (!isset($this->parts['{hint}'])) {
$this->parts['{hint}'] = '';
}
$content = strtr($this->template, $this->parts);
} elseif (!is_string($content)) {
$content = call_user_func($content, $this);
}
return $this->begin() . "\n" . $content . "\n" . $this->end();
}
/**
* @return self
*/
public function errorToLeft()
{
Html::addCssClass($this->errorOptions, 'left');
return $this;
}
/**
* @inheritdoc
*/
public function input($type, $options = [])
{
$options['title'] = $options['placeholder'] = $this->model->getAttributeLabel($this->attribute);
$options['ng-model'] = 'data.' . $this->attribute;
return parent::input($type, $options);
}
/**
* @inheritdoc
*/
public function textInput($options = [])
{
$options['title'] = $options['placeholder'] = $this->model->getAttributeLabel($this->attribute);
$options['ng-model'] = 'data.' . $this->attribute;
return parent::textInput($options);
}
/**
* @inheritdoc
*/
public function passwordInput($options = [])
{
$options['title'] = $options['placeholder'] = $this->model->getAttributeLabel($this->attribute);
$options['ng-model'] = 'data.' . $this->attribute;
return parent::passwordInput($options);
}
/**
* @inheritdoc
*/
public function textarea($options = [])
{
$options['title'] = $options['placeholder'] = $this->model->getAttributeLabel($this->attribute);
$options['ng-model'] = 'data.' . $this->attribute;
return parent::textarea($options);
}
/**
* Renders a email input.
* This method will generate the "name" and "value" tag attributes automatically for the model attribute
* unless they are explicitly specified in `$options`.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]].
* @return static the field object itself
*/
public function emailInput($options = [])
{
$options['placeholder'] = $this->model->getAttributeLabel($this->attribute);
$options['ng-model'] = 'data.' . $this->attribute;
return static::input('email', $options);
}
/**
* @inheritdoc
*/
public function checkbox($options = [])
{
Html::addCssClass($this->options, 'checkbox');
$options['iCheck'] = true;
$options['ng-model'] = 'data.' . $this->attribute;
$options['label'] = $this->model->getAttributeLabel($this->attribute);
$this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
return $this;
}
/**
* @inheritdoc
*/
public function checkboxList($items, $options = [], $item_options = [])
{
$options['item'] = function ($index, $label, $name, $checked, $value) use ($item_options) {
$options = [
'value' => $value,
'label' => $label,
'iCheck' => true,
'ng-model' => 'data.' . $this->attribute . '.' . $value,
];
if (isset($item_options[$value])) {
$options = array_merge($options, $item_options[$value]);
}
return Html::checkbox($name, $checked, $options);
};
return parent::checkboxList($items, $options);
}
/**
* Generates a tag that contains the first validation error of the specified model attribute.
* Note that even if there is no validation error, this method will still return an empty error tag.
* @param \yii\base\Model $model the model object
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
* about attribute expression.
* @param array $options the tag options in terms of name-value pairs. The values will be HTML-encoded
* using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
*
* The following options are specially handled:
*
* - tag: this specifies the tag name. If not set, "div" will be used.
* - encode: boolean, if set to false then value won't be encoded.
*
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
*
* @return string the generated label tag
*/
private function _error($model, $attribute, $options)
{
$attribute = Html::getAttributeName($attribute);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$options['ng-show'] = 'error.' . $attribute;
return Html::tag($tag, '{{ error.' . $attribute . ' }}', $options);
}
}