-
Notifications
You must be signed in to change notification settings - Fork 0
/
FnActiveField.php
259 lines (226 loc) · 8.42 KB
/
FnActiveField.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* @link http://foundationize.com
* @package shqear/yii2-foundation6
*/
namespace shqear\foundation6;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Description of ActiveField
*/
class FnActiveField extends \yii\widgets\ActiveField
{
public $template = "{beginLabel}{labelTitle}\n{input}{endLabel}\n{error}\n{hint}\n";
public $inlineTemplate = "<div class=\"row\"><div class=\"small-3 columns\">{label}</div><div class=\"small-9 columns\">{input}\n{error}\n{hint}</div></div>\n";
public $checkboxTemplate = "{input}\n{label}\n{error}\n{hint}\n";
public $checkboxListTemplate = "{label}\n{input}\n{error}\n{hint}\n";
public $radioTemplate = "{label}\n{input}\n{error}\n{hint}\n";
public $radioListTemplate = "{label}\n{input}\n{error}\n{hint}\n";
/**
* @var string|null optional template to render the `{input}` placeholder content
*/
public $inputTemplate;
/**
* @var array options for the wrapper tag, used in the `{beginWrapper}` placeholder
*/
public $wrapperOptions = [];
/**
* @var bool whether to render the error. Default is `true` except for layout `inline`.
*/
public $enableError = true;
/**
* @var bool whether to render the label. Default is `true`.
*/
public $enableLabel = true;
/**
*
* @var array
*/
public $inputOptions = [];
/**
* @inheritdoc
*/
public function __construct($config = [])
{
$layoutConfig = $this->createLayoutConfig($config);
$config = ArrayHelper::merge($layoutConfig, $config);
return parent::__construct($config);
}
/**
* Renders the hint tag.
* @param string $content the hint content. It will NOT be HTML-encoded.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the hint tag. The values will be HTML-encoded using [[Html::encode()]].
*
* The following options are specially handled:
*
* - tag: this specifies the tag name. If not set, "div" will be used.
*
* @return static the field object itself
*/
public function hint($content, $options = [])
{
$options = array_merge($this->hintOptions, $options, [
'id' => 'hint-' . Html::getInputId($this->model, $this->attribute)
]);
$tag = ArrayHelper::remove($options, 'tag', 'p');
$this->parts['{hint}'] = Html::tag($tag, $content, $options);
return $this;
}
/**
* Renders an input tag.
* @param string $type the input type (e.g. 'text', 'password')
* @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 input($type, $options = [])
{
$options = array_merge($this->inputOptions, [
'class' => 'hint-' . Html::getInputId($this->model, $this->attribute)
]);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeInput($type, $this->model, $this->attribute, $options);
return $this;
}
/**
* Renders a text 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 textInput($options = [])
{
$options = array_merge($this->inputOptions, $options, [
'aria-describedby' => 'hint-' . Html::getInputId($this->model, $this->attribute)
]);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeTextInput($this->model, $this->attribute, $options);
return $this;
}
/**
* @inheritdoc
*/
public function checkbox($options = [], $enclosedByLabel = false)
{
if ($enclosedByLabel) {
$this->template = $this->form->layout === 'inline' ?
$this->inlineTemplate : $this->checkboxTemplate;
}
$this->template = $this->checkboxTemplate;
return parent::checkbox($options, $enclosedByLabel);
}
/**
* @inheritdoc
*/
public function checkboxList($items, $options = [])
{
$this->template = $this->form->layout === 'inline' ?
$this->inlineTemplate : $this->checkboxListTemplate;
if (!isset($options['item'])) {
$options['item'] = function ($index, $label, $name, $checked, $value) {
return Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]);
};
}
parent::checkboxList($items, $options);
return $this;
}
/**
* @inheritdoc
*/
public function radio($options = [], $enclosedByLabel = false)
{
$this->template = $this->form->layout === 'inline' ?
$this->inlineTemplate : $this->radioTemplate;
return parent::radio($options, $enclosedByLabel);
}
/**
* @inheritdoc
*/
public function radioList($items, $options = [])
{
$this->template = $this->form->layout === 'inline' ?
$this->inlineTemplate : $this->radioListTemplate;
if (!isset($options['item'])) {
$options['item'] = function ($index, $label, $name, $checked, $value) {
return Html::radio($name, $checked, ['label' => $label, 'value' => $value]);
};
}
parent::radioList($items, $options);
return $this;
}
/**
* @inheritdoc
*/
public function render($content = null)
{
if ($content === null) {
if ($this->enableLabel === false) {
$this->parts['{label}'] = '';
$this->parts['{beginLabel}'] = '';
$this->parts['{labelTitle}'] = '';
$this->parts['{endLabel}'] = '';
} elseif (!isset($this->parts['{beginLabel}'])) {
$this->renderLabelParts();
}
if ($this->enableError === false) {
$this->parts['{error}'] = '';
}
if ($this->inputTemplate) {
$input = isset($this->parts['{input}']) ?
$this->parts['{input}'] : Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
$this->parts['{input}'] = strtr($this->inputTemplate, ['{input}' => $input]);
}
}
return parent::render($content);
}
/**
* @param array $instanceConfig the configuration passed to this instance's constructor
* @return array the layout specific default configuration for this instance
*/
protected function createLayoutConfig($instanceConfig)
{
$config = [
'hintOptions' => [
'tag' => 'p',
'class' => 'help-text'
],
'errorOptions' => [
'tag' => 'small',
'class' => 'error-box error'
]
];
$layout = $instanceConfig['form']->layout;
if ($layout === 'default') {
$config['template'] = $this->template;
} elseif ($layout === 'inline') {
$config['template'] = $this->inlineTemplate;
$config['labelOptions'] = ['class' => 'right inline'];
$config['enableError'] = false;
}
return $config;
}
/**
* @param string|null $label the label or null to use model label
* @param array $options the tag options
*/
protected function renderLabelParts($label = null, $options = [])
{
$options = array_merge($this->labelOptions, $options);
if ($label === null) {
if (isset($options['label'])) {
$label = $options['label'];
unset($options['label']);
} else {
$attribute = Html::getAttributeName($this->attribute);
$label = Html::encode($this->model->getAttributeLabel($attribute));
}
}
$this->parts['{beginLabel}'] = Html::beginTag('label', $options);
$this->parts['{endLabel}'] = Html::endTag('label');
$this->parts['{labelTitle}'] = $label;
}
}