1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license https://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace yii\helpers; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\InvalidArgumentException; |
13
|
|
|
use yii\base\Model; |
14
|
|
|
use yii\db\ActiveRecordInterface; |
15
|
|
|
use yii\validators\StringValidator; |
16
|
|
|
use yii\web\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* BaseHtml provides concrete implementation for [[Html]]. |
20
|
|
|
* |
21
|
|
|
* Do not use BaseHtml. Use [[Html]] instead. |
22
|
|
|
* |
23
|
|
|
* @author Qiang Xue <[email protected]> |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
class BaseHtml |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string Regular expression used for attribute name validation. |
30
|
|
|
* @since 2.0.12 |
31
|
|
|
*/ |
32
|
|
|
public static $attributeRegex = '/(^|.*\])([\w\.\+]+)(\[.*|$)/u'; |
33
|
|
|
/** |
34
|
|
|
* @var array list of void elements (element name => 1) |
35
|
|
|
* @see https://html.spec.whatwg.org/multipage/syntax.html#void-element |
36
|
|
|
*/ |
37
|
|
|
public static $voidElements = [ |
38
|
|
|
'area' => 1, |
39
|
|
|
'base' => 1, |
40
|
|
|
'br' => 1, |
41
|
|
|
'col' => 1, |
42
|
|
|
'command' => 1, |
43
|
|
|
'embed' => 1, |
44
|
|
|
'hr' => 1, |
45
|
|
|
'img' => 1, |
46
|
|
|
'input' => 1, |
47
|
|
|
'keygen' => 1, |
48
|
|
|
'link' => 1, |
49
|
|
|
'meta' => 1, |
50
|
|
|
'param' => 1, |
51
|
|
|
'source' => 1, |
52
|
|
|
'track' => 1, |
53
|
|
|
'wbr' => 1, |
54
|
|
|
]; |
55
|
|
|
/** |
56
|
|
|
* @var array the preferred order of attributes in a tag. This mainly affects the order of the attributes |
57
|
|
|
* that are rendered by [[renderTagAttributes()]]. |
58
|
|
|
*/ |
59
|
|
|
public static $attributeOrder = [ |
60
|
|
|
'type', |
61
|
|
|
'id', |
62
|
|
|
'class', |
63
|
|
|
'name', |
64
|
|
|
'value', |
65
|
|
|
|
66
|
|
|
'href', |
67
|
|
|
'src', |
68
|
|
|
'srcset', |
69
|
|
|
'form', |
70
|
|
|
'action', |
71
|
|
|
'method', |
72
|
|
|
|
73
|
|
|
'selected', |
74
|
|
|
'checked', |
75
|
|
|
'readonly', |
76
|
|
|
'disabled', |
77
|
|
|
'multiple', |
78
|
|
|
|
79
|
|
|
'size', |
80
|
|
|
'maxlength', |
81
|
|
|
'width', |
82
|
|
|
'height', |
83
|
|
|
'rows', |
84
|
|
|
'cols', |
85
|
|
|
|
86
|
|
|
'alt', |
87
|
|
|
'title', |
88
|
|
|
'rel', |
89
|
|
|
'media', |
90
|
|
|
]; |
91
|
|
|
/** |
92
|
|
|
* @var array list of tag attributes that should be specially handled when their values are of array type. |
93
|
|
|
* In particular, if the value of the `data` attribute is `['name' => 'xyz', 'age' => 13]`, two attributes |
94
|
|
|
* will be generated instead of one: `data-name="xyz" data-age="13"`. |
95
|
|
|
* @since 2.0.3 |
96
|
|
|
*/ |
97
|
|
|
public static $dataAttributes = ['aria', 'data', 'data-ng', 'ng']; |
98
|
|
|
/** |
99
|
|
|
* @var bool whether to removes duplicate class names in tag attribute `class` |
100
|
|
|
* @see mergeCssClasses() |
101
|
|
|
* @see renderTagAttributes() |
102
|
|
|
* @since 2.0.44 |
103
|
|
|
*/ |
104
|
|
|
public static $normalizeClassAttribute = false; |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Encodes special characters into HTML entities. |
109
|
|
|
* The [[\yii\base\Application::charset|application charset]] will be used for encoding. |
110
|
|
|
* @param string $content the content to be encoded |
111
|
|
|
* @param bool $doubleEncode whether to encode HTML entities in `$content`. If false, |
112
|
|
|
* HTML entities in `$content` will not be further encoded. |
113
|
|
|
* @return string the encoded content |
114
|
|
|
* @see decode() |
115
|
|
|
* @see https://www.php.net/manual/en/function.htmlspecialchars.php |
116
|
|
|
*/ |
117
|
284 |
|
public static function encode($content, $doubleEncode = true) |
118
|
|
|
{ |
119
|
284 |
|
return htmlspecialchars((string)$content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app ? Yii::$app->charset : 'UTF-8', $doubleEncode); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Decodes special HTML entities back to the corresponding characters. |
124
|
|
|
* This is the opposite of [[encode()]]. |
125
|
|
|
* @param string $content the content to be decoded |
126
|
|
|
* @return string the decoded content |
127
|
|
|
* @see encode() |
128
|
|
|
* @see https://www.php.net/manual/en/function.htmlspecialchars-decode.php |
129
|
|
|
*/ |
130
|
1 |
|
public static function decode($content) |
131
|
|
|
{ |
132
|
1 |
|
return htmlspecialchars_decode($content, ENT_QUOTES); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Generates a complete HTML tag. |
137
|
|
|
* @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
138
|
|
|
* @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded. |
139
|
|
|
* If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks. |
140
|
|
|
* @param array $options the HTML tag attributes (HTML options) in terms of name-value pairs. |
141
|
|
|
* These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
142
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
143
|
|
|
* |
144
|
|
|
* For example when using `['class' => 'my-class', 'target' => '_blank', 'value' => null]` it will result in the |
145
|
|
|
* html attributes rendered like this: `class="my-class" target="_blank"`. |
146
|
|
|
* |
147
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
148
|
|
|
* |
149
|
|
|
* @return string the generated HTML tag |
150
|
|
|
* @see beginTag() |
151
|
|
|
* @see endTag() |
152
|
|
|
*/ |
153
|
270 |
|
public static function tag($name, $content = '', $options = []) |
154
|
|
|
{ |
155
|
270 |
|
if ($name === null || $name === false) { |
156
|
3 |
|
return $content; |
157
|
|
|
} |
158
|
269 |
|
$html = "<$name" . static::renderTagAttributes($options) . '>'; |
159
|
269 |
|
return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>"; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Generates a start tag. |
164
|
|
|
* @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
165
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
166
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
167
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
168
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
169
|
|
|
* @return string the generated start tag |
170
|
|
|
* @see endTag() |
171
|
|
|
* @see tag() |
172
|
|
|
*/ |
173
|
53 |
|
public static function beginTag($name, $options = []) |
174
|
|
|
{ |
175
|
53 |
|
if ($name === null || $name === false) { |
176
|
3 |
|
return ''; |
177
|
|
|
} |
178
|
|
|
|
179
|
53 |
|
return "<$name" . static::renderTagAttributes($options) . '>'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Generates an end tag. |
184
|
|
|
* @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
185
|
|
|
* @return string the generated end tag |
186
|
|
|
* @see beginTag() |
187
|
|
|
* @see tag() |
188
|
|
|
*/ |
189
|
19 |
|
public static function endTag($name) |
190
|
|
|
{ |
191
|
19 |
|
if ($name === null || $name === false) { |
192
|
3 |
|
return ''; |
193
|
|
|
} |
194
|
|
|
|
195
|
18 |
|
return "</$name>"; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Generates a style tag. |
200
|
|
|
* @param string $content the style content |
201
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
202
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
203
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
204
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
205
|
|
|
* @return string the generated style tag |
206
|
|
|
*/ |
207
|
1 |
|
public static function style($content, $options = []) |
208
|
|
|
{ |
209
|
1 |
|
return static::tag('style', $content, $options); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Generates a script tag. |
214
|
|
|
* @param string $content the script content |
215
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
216
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
217
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
218
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
219
|
|
|
* @return string the generated script tag |
220
|
|
|
*/ |
221
|
3 |
|
public static function script($content, $options = []) |
222
|
|
|
{ |
223
|
3 |
|
$view = Yii::$app->getView(); |
224
|
3 |
|
if ($view instanceof \yii\web\View && !empty($view->scriptOptions)) { |
225
|
1 |
|
$options = array_merge($view->scriptOptions, $options); |
226
|
|
|
} |
227
|
|
|
|
228
|
3 |
|
return static::tag('script', $content, $options); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Generates a link tag that refers to an external CSS file. |
233
|
|
|
* @param array|string $url the URL of the external CSS file. This parameter will be processed by [[Url::to()]]. |
234
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
235
|
|
|
* |
236
|
|
|
* - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified, |
237
|
|
|
* the generated `link` tag will be enclosed within the conditional comments. This is mainly useful |
238
|
|
|
* for supporting old versions of IE browsers. |
239
|
|
|
* - noscript: if set to true, `link` tag will be wrapped into `<noscript>` tags. |
240
|
|
|
* |
241
|
|
|
* The rest of the options will be rendered as the attributes of the resulting link tag. The values will |
242
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
243
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
244
|
|
|
* @return string the generated link tag |
245
|
|
|
* @see Url::to() |
246
|
|
|
*/ |
247
|
21 |
|
public static function cssFile($url, $options = []) |
248
|
|
|
{ |
249
|
21 |
|
if (!isset($options['rel'])) { |
250
|
21 |
|
$options['rel'] = 'stylesheet'; |
251
|
|
|
} |
252
|
21 |
|
$options['href'] = Url::to($url); |
253
|
|
|
|
254
|
21 |
|
if (isset($options['condition'])) { |
255
|
1 |
|
$condition = $options['condition']; |
256
|
1 |
|
unset($options['condition']); |
257
|
1 |
|
return self::wrapIntoCondition(static::tag('link', '', $options), $condition); |
258
|
21 |
|
} elseif (isset($options['noscript']) && $options['noscript'] === true) { |
259
|
1 |
|
unset($options['noscript']); |
260
|
1 |
|
return '<noscript>' . static::tag('link', '', $options) . '</noscript>'; |
261
|
|
|
} |
262
|
|
|
|
263
|
21 |
|
return static::tag('link', '', $options); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Generates a script tag that refers to an external JavaScript file. |
268
|
|
|
* @param string $url the URL of the external JavaScript file. This parameter will be processed by [[Url::to()]]. |
269
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following option is specially handled: |
270
|
|
|
* |
271
|
|
|
* - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified, |
272
|
|
|
* the generated `script` tag will be enclosed within the conditional comments. This is mainly useful |
273
|
|
|
* for supporting old versions of IE browsers. |
274
|
|
|
* |
275
|
|
|
* The rest of the options will be rendered as the attributes of the resulting script tag. The values will |
276
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
277
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
278
|
|
|
* @return string the generated script tag |
279
|
|
|
* @see Url::to() |
280
|
|
|
*/ |
281
|
25 |
|
public static function jsFile($url, $options = []) |
282
|
|
|
{ |
283
|
25 |
|
$options['src'] = Url::to($url); |
284
|
25 |
|
if (isset($options['condition'])) { |
285
|
1 |
|
$condition = $options['condition']; |
286
|
1 |
|
unset($options['condition']); |
287
|
1 |
|
return self::wrapIntoCondition(static::tag('script', '', $options), $condition); |
288
|
|
|
} |
289
|
|
|
|
290
|
25 |
|
return static::tag('script', '', $options); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Wraps given content into conditional comments for IE, e.g., `lt IE 9`. |
295
|
|
|
* @param string $content raw HTML content. |
296
|
|
|
* @param string $condition condition string. |
297
|
|
|
* @return string generated HTML. |
298
|
|
|
*/ |
299
|
2 |
|
private static function wrapIntoCondition($content, $condition) |
300
|
|
|
{ |
301
|
2 |
|
if (strpos($condition, '!IE') !== false) { |
302
|
2 |
|
return "<!--[if $condition]><!-->\n" . $content . "\n<!--<![endif]-->"; |
303
|
|
|
} |
304
|
|
|
|
305
|
2 |
|
return "<!--[if $condition]>\n" . $content . "\n<![endif]-->"; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Generates the meta tags containing CSRF token information. |
310
|
|
|
* @return string the generated meta tags |
311
|
|
|
* @see Request::enableCsrfValidation |
312
|
|
|
*/ |
313
|
4 |
|
public static function csrfMetaTags() |
314
|
|
|
{ |
315
|
4 |
|
$request = Yii::$app->getRequest(); |
316
|
4 |
|
if ($request instanceof Request && $request->enableCsrfValidation) { |
317
|
3 |
|
return static::tag('meta', '', ['name' => 'csrf-param', 'content' => $request->csrfParam]) . "\n" |
318
|
3 |
|
. static::tag('meta', '', ['name' => 'csrf-token', 'content' => $request->getCsrfToken()]) . "\n"; |
319
|
|
|
} |
320
|
|
|
|
321
|
1 |
|
return ''; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Generates a form start tag. |
326
|
|
|
* @param array|string $action the form action URL. This parameter will be processed by [[Url::to()]]. |
327
|
|
|
* @param string $method the form submission method, such as "post", "get", "put", "delete" (case-insensitive). |
328
|
|
|
* Since most browsers only support "post" and "get", if other methods are given, they will |
329
|
|
|
* be simulated using "post", and a hidden input will be added which contains the actual method type. |
330
|
|
|
* See [[\yii\web\Request::methodParam]] for more details. |
331
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
332
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
333
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
334
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
335
|
|
|
* |
336
|
|
|
* Special options: |
337
|
|
|
* |
338
|
|
|
* - `csrf`: whether to generate the CSRF hidden input. Defaults to true. |
339
|
|
|
* |
340
|
|
|
* @return string the generated form start tag. |
341
|
|
|
* @see endForm() |
342
|
|
|
*/ |
343
|
50 |
|
public static function beginForm($action = '', $method = 'post', $options = []) |
344
|
|
|
{ |
345
|
50 |
|
$action = Url::to($action); |
346
|
|
|
|
347
|
50 |
|
$hiddenInputs = []; |
348
|
|
|
|
349
|
50 |
|
$request = Yii::$app->getRequest(); |
350
|
50 |
|
if ($request instanceof Request) { |
351
|
45 |
|
if (strcasecmp($method, 'get') && strcasecmp($method, 'post')) { |
352
|
|
|
// simulate PUT, DELETE, etc. via POST |
353
|
4 |
|
$hiddenInputs[] = static::hiddenInput($request->methodParam, $method); |
354
|
4 |
|
$method = 'post'; |
355
|
|
|
} |
356
|
45 |
|
$csrf = ArrayHelper::remove($options, 'csrf', true); |
357
|
|
|
|
358
|
45 |
|
if ($csrf && $request->enableCsrfValidation && strcasecmp($method, 'post') === 0) { |
359
|
38 |
|
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken()); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
50 |
|
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) { |
364
|
|
|
// query parameters in the action are ignored for GET method |
365
|
|
|
// we use hidden fields to add them back |
366
|
1 |
|
foreach (explode('&', substr($action, $pos + 1)) as $pair) { |
367
|
1 |
|
if (($pos1 = strpos($pair, '=')) !== false) { |
368
|
1 |
|
$hiddenInputs[] = static::hiddenInput( |
369
|
1 |
|
urldecode(substr($pair, 0, $pos1)), |
370
|
1 |
|
urldecode(substr($pair, $pos1 + 1)) |
371
|
1 |
|
); |
372
|
|
|
} else { |
373
|
1 |
|
$hiddenInputs[] = static::hiddenInput(urldecode($pair), ''); |
374
|
|
|
} |
375
|
|
|
} |
376
|
1 |
|
$action = substr($action, 0, $pos); |
377
|
|
|
} |
378
|
|
|
|
379
|
50 |
|
$options['action'] = $action; |
380
|
50 |
|
$options['method'] = $method; |
381
|
50 |
|
$form = static::beginTag('form', $options); |
382
|
50 |
|
if (!empty($hiddenInputs)) { |
383
|
43 |
|
$form .= "\n" . implode("\n", $hiddenInputs); |
384
|
|
|
} |
385
|
|
|
|
386
|
50 |
|
return $form; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Generates a form end tag. |
391
|
|
|
* @return string the generated tag |
392
|
|
|
* @see beginForm() |
393
|
|
|
*/ |
394
|
43 |
|
public static function endForm() |
395
|
|
|
{ |
396
|
43 |
|
return '</form>'; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Generates a hyperlink tag. |
401
|
|
|
* @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
402
|
|
|
* such as an image tag. If this is coming from end users, you should consider [[encode()]] |
403
|
|
|
* it to prevent XSS attacks. |
404
|
|
|
* @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[Url::to()]] |
405
|
|
|
* and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute |
406
|
|
|
* will not be generated. |
407
|
|
|
* |
408
|
|
|
* If you want to use an absolute url you can call [[Url::to()]] yourself, before passing the URL to this method, |
409
|
|
|
* like this: |
410
|
|
|
* |
411
|
|
|
* ```php |
412
|
|
|
* Html::a('link text', Url::to($url, true)) |
413
|
|
|
* ``` |
414
|
|
|
* |
415
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
416
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
417
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
418
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
419
|
|
|
* @return string the generated hyperlink |
420
|
|
|
* @see \yii\helpers\Url::to() |
421
|
|
|
*/ |
422
|
31 |
|
public static function a($text, $url = null, $options = []) |
423
|
|
|
{ |
424
|
31 |
|
if ($url !== null) { |
425
|
31 |
|
$options['href'] = Url::to($url); |
426
|
|
|
} |
427
|
|
|
|
428
|
31 |
|
return static::tag('a', $text, $options); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Generates a mailto hyperlink. |
433
|
|
|
* @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
434
|
|
|
* such as an image tag. If this is coming from end users, you should consider [[encode()]] |
435
|
|
|
* it to prevent XSS attacks. |
436
|
|
|
* @param string|null $email email address. If this is null, the first parameter (link body) will be treated |
437
|
|
|
* as the email address and used. |
438
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
439
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
440
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
441
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
442
|
|
|
* @return string the generated mailto link |
443
|
|
|
*/ |
444
|
2 |
|
public static function mailto($text, $email = null, $options = []) |
445
|
|
|
{ |
446
|
2 |
|
$options['href'] = 'mailto:' . ($email === null ? $text : $email); |
447
|
2 |
|
return static::tag('a', $text, $options); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Generates an image tag. |
452
|
|
|
* @param array|string $src the image URL. This parameter will be processed by [[Url::to()]]. |
453
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
454
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
455
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
456
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
457
|
|
|
* |
458
|
|
|
* Since version 2.0.12 It is possible to pass the `srcset` option as an array which keys are |
459
|
|
|
* descriptors and values are URLs. All URLs will be processed by [[Url::to()]]. |
460
|
|
|
* @return string the generated image tag. |
461
|
|
|
*/ |
462
|
10 |
|
public static function img($src, $options = []) |
463
|
|
|
{ |
464
|
10 |
|
$options['src'] = Url::to($src); |
465
|
|
|
|
466
|
10 |
|
if (isset($options['srcset']) && is_array($options['srcset'])) { |
467
|
5 |
|
$srcset = []; |
468
|
5 |
|
foreach ($options['srcset'] as $descriptor => $url) { |
469
|
4 |
|
$srcset[] = Url::to($url) . ' ' . $descriptor; |
470
|
|
|
} |
471
|
5 |
|
$options['srcset'] = implode(',', $srcset); |
472
|
|
|
} |
473
|
|
|
|
474
|
10 |
|
if (!isset($options['alt'])) { |
475
|
9 |
|
$options['alt'] = ''; |
476
|
|
|
} |
477
|
|
|
|
478
|
10 |
|
return static::tag('img', '', $options); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Generates a label tag. |
483
|
|
|
* @param string $content label text. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
484
|
|
|
* such as an image tag. If this is is coming from end users, you should [[encode()]] |
485
|
|
|
* it to prevent XSS attacks. |
486
|
|
|
* @param string|null $for the ID of the HTML element that this label is associated with. |
487
|
|
|
* If this is null, the "for" attribute will not be generated. |
488
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
489
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
490
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
491
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
492
|
|
|
* @return string the generated label tag |
493
|
|
|
*/ |
494
|
31 |
|
public static function label($content, $for = null, $options = []) |
495
|
|
|
{ |
496
|
31 |
|
$options['for'] = $for; |
497
|
31 |
|
return static::tag('label', $content, $options); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Generates a button tag. |
502
|
|
|
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
503
|
|
|
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
504
|
|
|
* you should consider [[encode()]] it to prevent XSS attacks. |
505
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
506
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
507
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
508
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
509
|
|
|
* @return string the generated button tag |
510
|
|
|
*/ |
511
|
3 |
|
public static function button($content = 'Button', $options = []) |
512
|
|
|
{ |
513
|
3 |
|
if (!isset($options['type'])) { |
514
|
1 |
|
$options['type'] = 'button'; |
515
|
|
|
} |
516
|
|
|
|
517
|
3 |
|
return static::tag('button', $content, $options); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Generates a submit button tag. |
522
|
|
|
* |
523
|
|
|
* Be careful when naming form elements such as submit buttons. According to the [jQuery documentation](https://api.jquery.com/submit/) there |
524
|
|
|
* are some reserved names that can cause conflicts, e.g. `submit`, `length`, or `method`. |
525
|
|
|
* |
526
|
|
|
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
527
|
|
|
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
528
|
|
|
* you should consider [[encode()]] it to prevent XSS attacks. |
529
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
530
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
531
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
532
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
533
|
|
|
* @return string the generated submit button tag |
534
|
|
|
*/ |
535
|
1 |
|
public static function submitButton($content = 'Submit', $options = []) |
536
|
|
|
{ |
537
|
1 |
|
$options['type'] = 'submit'; |
538
|
1 |
|
return static::button($content, $options); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Generates a reset button tag. |
543
|
|
|
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
544
|
|
|
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
545
|
|
|
* you should consider [[encode()]] it to prevent XSS attacks. |
546
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
547
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
548
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
549
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
550
|
|
|
* @return string the generated reset button tag |
551
|
|
|
*/ |
552
|
1 |
|
public static function resetButton($content = 'Reset', $options = []) |
553
|
|
|
{ |
554
|
1 |
|
$options['type'] = 'reset'; |
555
|
1 |
|
return static::button($content, $options); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Generates an input type of the given type. |
560
|
|
|
* @param string $type the type attribute. |
561
|
|
|
* @param string|null $name the name attribute. If it is null, the name attribute will not be generated. |
562
|
|
|
* @param string|null $value the value attribute. If it is null, the value attribute will not be generated. |
563
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
564
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
565
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
566
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
567
|
|
|
* @return string the generated input tag |
568
|
|
|
*/ |
569
|
98 |
|
public static function input($type, $name = null, $value = null, $options = []) |
570
|
|
|
{ |
571
|
98 |
|
if (!isset($options['type'])) { |
572
|
98 |
|
$options['type'] = $type; |
573
|
|
|
} |
574
|
98 |
|
$options['name'] = $name; |
575
|
98 |
|
$options['value'] = $value === null ? null : (string) $value; |
576
|
98 |
|
return static::tag('input', '', $options); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Generates an input button. |
581
|
|
|
* @param string|null $label the value attribute. If it is null, the value attribute will not be generated. |
582
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
583
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
584
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
585
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
586
|
|
|
* @return string the generated button tag |
587
|
|
|
*/ |
588
|
1 |
|
public static function buttonInput($label = 'Button', $options = []) |
589
|
|
|
{ |
590
|
1 |
|
$options['type'] = 'button'; |
591
|
1 |
|
$options['value'] = $label; |
592
|
1 |
|
return static::tag('input', '', $options); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Generates a submit input button. |
597
|
|
|
* |
598
|
|
|
* Be careful when naming form elements such as submit buttons. According to the [jQuery documentation](https://api.jquery.com/submit/) there |
599
|
|
|
* are some reserved names that can cause conflicts, e.g. `submit`, `length`, or `method`. |
600
|
|
|
* |
601
|
|
|
* @param string|null $label the value attribute. If it is null, the value attribute will not be generated. |
602
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
603
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
604
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
605
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
606
|
|
|
* @return string the generated button tag |
607
|
|
|
*/ |
608
|
1 |
|
public static function submitInput($label = 'Submit', $options = []) |
609
|
|
|
{ |
610
|
1 |
|
$options['type'] = 'submit'; |
611
|
1 |
|
$options['value'] = $label; |
612
|
1 |
|
return static::tag('input', '', $options); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Generates a reset input button. |
617
|
|
|
* @param string|null $label the value attribute. If it is null, the value attribute will not be generated. |
618
|
|
|
* @param array $options the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. |
619
|
|
|
* Attributes whose value is null will be ignored and not put in the tag returned. |
620
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
621
|
|
|
* @return string the generated button tag |
622
|
|
|
*/ |
623
|
1 |
|
public static function resetInput($label = 'Reset', $options = []) |
624
|
|
|
{ |
625
|
1 |
|
$options['type'] = 'reset'; |
626
|
1 |
|
$options['value'] = $label; |
627
|
1 |
|
return static::tag('input', '', $options); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Generates a text input field. |
632
|
|
|
* @param string $name the name attribute. |
633
|
|
|
* @param string|null $value the value attribute. If it is null, the value attribute will not be generated. |
634
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
635
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
636
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
637
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
638
|
|
|
* @return string the generated text input tag |
639
|
|
|
*/ |
640
|
1 |
|
public static function textInput($name, $value = null, $options = []) |
641
|
|
|
{ |
642
|
1 |
|
return static::input('text', $name, $value, $options); |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Generates a hidden input field. |
647
|
|
|
* @param string $name the name attribute. |
648
|
|
|
* @param string|null $value the value attribute. If it is null, the value attribute will not be generated. |
649
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
650
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
651
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
652
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
653
|
|
|
* @return string the generated hidden input tag |
654
|
|
|
*/ |
655
|
59 |
|
public static function hiddenInput($name, $value = null, $options = []) |
656
|
|
|
{ |
657
|
59 |
|
return static::input('hidden', $name, $value, $options); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Generates a password input field. |
662
|
|
|
* @param string $name the name attribute. |
663
|
|
|
* @param string|null $value the value attribute. If it is null, the value attribute will not be generated. |
664
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
665
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
666
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
667
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
668
|
|
|
* @return string the generated password input tag |
669
|
|
|
*/ |
670
|
1 |
|
public static function passwordInput($name, $value = null, $options = []) |
671
|
|
|
{ |
672
|
1 |
|
return static::input('password', $name, $value, $options); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Generates a file input field. |
677
|
|
|
* To use a file input field, you should set the enclosing form's "enctype" attribute to |
678
|
|
|
* be "multipart/form-data". After the form is submitted, the uploaded file information |
679
|
|
|
* can be obtained via $_FILES[$name] (see PHP documentation). |
680
|
|
|
* @param string $name the name attribute. |
681
|
|
|
* @param string|null $value the value attribute. If it is null, the value attribute will not be generated. |
682
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
683
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
684
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
685
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
686
|
|
|
* @return string the generated file input tag |
687
|
|
|
*/ |
688
|
1 |
|
public static function fileInput($name, $value = null, $options = []) |
689
|
|
|
{ |
690
|
1 |
|
return static::input('file', $name, $value, $options); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* Generates a text area input. |
695
|
|
|
* @param string $name the input name |
696
|
|
|
* @param string $value the input value. Note that it will be encoded using [[encode()]]. |
697
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
698
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
699
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
700
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
701
|
|
|
* The following special options are recognized: |
702
|
|
|
* |
703
|
|
|
* - `doubleEncode`: whether to double encode HTML entities in `$value`. If `false`, HTML entities in `$value` will not |
704
|
|
|
* be further encoded. This option is available since version 2.0.11. |
705
|
|
|
* |
706
|
|
|
* @return string the generated text area tag |
707
|
|
|
*/ |
708
|
8 |
|
public static function textarea($name, $value = '', $options = []) |
709
|
|
|
{ |
710
|
8 |
|
$options['name'] = $name; |
711
|
8 |
|
$doubleEncode = ArrayHelper::remove($options, 'doubleEncode', true); |
712
|
8 |
|
return static::tag('textarea', static::encode($value, $doubleEncode), $options); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Generates a radio button input. |
717
|
|
|
* @param string $name the name attribute. |
718
|
|
|
* @param bool $checked whether the radio button should be checked. |
719
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
720
|
|
|
* See [[booleanInput()]] for details about accepted attributes. |
721
|
|
|
* |
722
|
|
|
* @return string the generated radio button tag |
723
|
|
|
*/ |
724
|
14 |
|
public static function radio($name, $checked = false, $options = []) |
725
|
|
|
{ |
726
|
14 |
|
return static::booleanInput('radio', $name, $checked, $options); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Generates a checkbox input. |
731
|
|
|
* @param string $name the name attribute. |
732
|
|
|
* @param bool $checked whether the checkbox should be checked. |
733
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
734
|
|
|
* See [[booleanInput()]] for details about accepted attributes. |
735
|
|
|
* |
736
|
|
|
* @return string the generated checkbox tag |
737
|
|
|
*/ |
738
|
13 |
|
public static function checkbox($name, $checked = false, $options = []) |
739
|
|
|
{ |
740
|
13 |
|
return static::booleanInput('checkbox', $name, $checked, $options); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Generates a boolean input. |
745
|
|
|
* @param string $type the input type. This can be either `radio` or `checkbox`. |
746
|
|
|
* @param string $name the name attribute. |
747
|
|
|
* @param bool $checked whether the checkbox should be checked. |
748
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
749
|
|
|
* |
750
|
|
|
* - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute |
751
|
|
|
* is present, a hidden input will be generated so that if the checkbox is not checked and is submitted, |
752
|
|
|
* the value of this attribute will still be submitted to the server via the hidden input. |
753
|
|
|
* - label: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass |
754
|
|
|
* in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. |
755
|
|
|
* When this option is specified, the checkbox will be enclosed by a label tag. |
756
|
|
|
* - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. |
757
|
|
|
* |
758
|
|
|
* The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will |
759
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
760
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
761
|
|
|
* |
762
|
|
|
* @return string the generated checkbox tag |
763
|
|
|
* @since 2.0.9 |
764
|
|
|
*/ |
765
|
26 |
|
protected static function booleanInput($type, $name, $checked = false, $options = []) |
766
|
|
|
{ |
767
|
|
|
// 'checked' option has priority over $checked argument |
768
|
26 |
|
if (!isset($options['checked'])) { |
769
|
25 |
|
$options['checked'] = (bool) $checked; |
770
|
|
|
} |
771
|
26 |
|
$value = array_key_exists('value', $options) ? $options['value'] : '1'; |
772
|
26 |
|
if (isset($options['uncheck'])) { |
773
|
|
|
// add a hidden field so that if the checkbox is not selected, it still submits a value |
774
|
7 |
|
$hiddenOptions = []; |
775
|
7 |
|
if (isset($options['form'])) { |
776
|
1 |
|
$hiddenOptions['form'] = $options['form']; |
777
|
|
|
} |
778
|
|
|
// make sure disabled input is not sending any value |
779
|
7 |
|
if (!empty($options['disabled'])) { |
780
|
2 |
|
$hiddenOptions['disabled'] = $options['disabled']; |
781
|
|
|
} |
782
|
7 |
|
$hidden = static::hiddenInput($name, $options['uncheck'], $hiddenOptions); |
783
|
7 |
|
unset($options['uncheck']); |
784
|
|
|
} else { |
785
|
21 |
|
$hidden = ''; |
786
|
|
|
} |
787
|
26 |
|
if (isset($options['label'])) { |
788
|
15 |
|
$label = $options['label']; |
789
|
15 |
|
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; |
790
|
15 |
|
unset($options['label'], $options['labelOptions']); |
791
|
15 |
|
$content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions); |
792
|
15 |
|
return $hidden . $content; |
793
|
|
|
} |
794
|
|
|
|
795
|
15 |
|
return $hidden . static::input($type, $name, $value, $options); |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
/** |
799
|
|
|
* Generates a drop-down list. |
800
|
|
|
* @param string $name the input name |
801
|
|
|
* @param string|bool|array|null $selection the selected value(s). String/boolean for single or array for multiple |
802
|
|
|
* selection(s). |
803
|
|
|
* @param array $items the option data items. The array keys are option values, and the array values |
804
|
|
|
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
805
|
|
|
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
806
|
|
|
* If you have a list of data models, you may convert them into the format described above using |
807
|
|
|
* [[\yii\helpers\ArrayHelper::map()]]. |
808
|
|
|
* |
809
|
|
|
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
810
|
|
|
* the labels will also be HTML-encoded. |
811
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
812
|
|
|
* |
813
|
|
|
* - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
814
|
|
|
* to override the value and to set other tag attributes: |
815
|
|
|
* |
816
|
|
|
* ```php |
817
|
|
|
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
818
|
|
|
* ``` |
819
|
|
|
* |
820
|
|
|
* - options: array, the attributes for the select option tags. The array keys must be valid option values, |
821
|
|
|
* and the array values are the extra attributes for the corresponding option tags. For example, |
822
|
|
|
* |
823
|
|
|
* ```php |
824
|
|
|
* [ |
825
|
|
|
* 'value1' => ['disabled' => true], |
826
|
|
|
* 'value2' => ['label' => 'value 2'], |
827
|
|
|
* ]; |
828
|
|
|
* ``` |
829
|
|
|
* |
830
|
|
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
831
|
|
|
* except that the array keys represent the optgroup labels specified in $items. |
832
|
|
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
833
|
|
|
* Defaults to false. |
834
|
|
|
* - encode: bool, whether to encode option prompt and option value characters. |
835
|
|
|
* Defaults to `true`. This option is available since 2.0.3. |
836
|
|
|
* - strict: boolean, if `$selection` is an array and this value is true a strict comparison will be performed on `$items` keys. Defaults to false. |
837
|
|
|
* This option is available since 2.0.37. |
838
|
|
|
* |
839
|
|
|
* The rest of the options will be rendered as the attributes of the resulting tag. The values will |
840
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
841
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
842
|
|
|
* |
843
|
|
|
* @return string the generated drop-down list tag |
844
|
|
|
*/ |
845
|
19 |
|
public static function dropDownList($name, $selection = null, $items = [], $options = []) |
846
|
|
|
{ |
847
|
19 |
|
if (!empty($options['multiple'])) { |
848
|
1 |
|
return static::listBox($name, $selection, $items, $options); |
849
|
|
|
} |
850
|
19 |
|
$options['name'] = $name; |
851
|
19 |
|
unset($options['unselect']); |
852
|
19 |
|
$selectOptions = static::renderSelectOptions($selection, $items, $options); |
853
|
19 |
|
return static::tag('select', "\n" . $selectOptions . "\n", $options); |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* Generates a list box. |
858
|
|
|
* @param string $name the input name |
859
|
|
|
* @param string|bool|array|null $selection the selected value(s). String for single or array for multiple |
860
|
|
|
* selection(s). |
861
|
|
|
* @param array $items the option data items. The array keys are option values, and the array values |
862
|
|
|
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
863
|
|
|
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
864
|
|
|
* If you have a list of data models, you may convert them into the format described above using |
865
|
|
|
* [[\yii\helpers\ArrayHelper::map()]]. |
866
|
|
|
* |
867
|
|
|
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
868
|
|
|
* the labels will also be HTML-encoded. |
869
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
870
|
|
|
* |
871
|
|
|
* - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
872
|
|
|
* to override the value and to set other tag attributes: |
873
|
|
|
* |
874
|
|
|
* ```php |
875
|
|
|
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
876
|
|
|
* ``` |
877
|
|
|
* |
878
|
|
|
* - options: array, the attributes for the select option tags. The array keys must be valid option values, |
879
|
|
|
* and the array values are the extra attributes for the corresponding option tags. For example, |
880
|
|
|
* |
881
|
|
|
* ```php |
882
|
|
|
* [ |
883
|
|
|
* 'value1' => ['disabled' => true], |
884
|
|
|
* 'value2' => ['label' => 'value 2'], |
885
|
|
|
* ]; |
886
|
|
|
* ``` |
887
|
|
|
* |
888
|
|
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
889
|
|
|
* except that the array keys represent the optgroup labels specified in $items. |
890
|
|
|
* - unselect: string, the value that will be submitted when no option is selected. |
891
|
|
|
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple |
892
|
|
|
* mode, we can still obtain the posted unselect value. |
893
|
|
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
894
|
|
|
* Defaults to false. |
895
|
|
|
* - encode: bool, whether to encode option prompt and option value characters. |
896
|
|
|
* Defaults to `true`. This option is available since 2.0.3. |
897
|
|
|
* - strict: boolean, if `$selection` is an array and this value is true a strict comparison will be performed on `$items` keys. Defaults to false. |
898
|
|
|
* This option is available since 2.0.37. |
899
|
|
|
* |
900
|
|
|
* The rest of the options will be rendered as the attributes of the resulting tag. The values will |
901
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
902
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
903
|
|
|
* |
904
|
|
|
* @return string the generated list box tag |
905
|
|
|
*/ |
906
|
5 |
|
public static function listBox($name, $selection = null, $items = [], $options = []) |
907
|
|
|
{ |
908
|
5 |
|
if (!array_key_exists('size', $options)) { |
909
|
5 |
|
$options['size'] = 4; |
910
|
|
|
} |
911
|
5 |
|
if (!empty($options['multiple']) && !empty($name) && substr_compare($name, '[]', -2, 2)) { |
912
|
4 |
|
$name .= '[]'; |
913
|
|
|
} |
914
|
5 |
|
$options['name'] = $name; |
915
|
5 |
|
if (isset($options['unselect'])) { |
916
|
|
|
// add a hidden field so that if the list box has no option being selected, it still submits a value |
917
|
4 |
|
if (!empty($name) && substr_compare($name, '[]', -2, 2) === 0) { |
918
|
2 |
|
$name = substr($name, 0, -2); |
919
|
|
|
} |
920
|
4 |
|
$hiddenOptions = []; |
921
|
|
|
// make sure disabled input is not sending any value |
922
|
4 |
|
if (!empty($options['disabled'])) { |
923
|
1 |
|
$hiddenOptions['disabled'] = $options['disabled']; |
924
|
|
|
} |
925
|
4 |
|
$hidden = static::hiddenInput($name, $options['unselect'], $hiddenOptions); |
926
|
4 |
|
unset($options['unselect']); |
927
|
|
|
} else { |
928
|
2 |
|
$hidden = ''; |
929
|
|
|
} |
930
|
5 |
|
$selectOptions = static::renderSelectOptions($selection, $items, $options); |
931
|
5 |
|
return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options); |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
/** |
935
|
|
|
* Generates a list of checkboxes. |
936
|
|
|
* A checkbox list allows multiple selection, like [[listBox()]]. |
937
|
|
|
* As a result, the corresponding submitted value is an array. |
938
|
|
|
* @param string $name the name attribute of each checkbox. |
939
|
|
|
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
940
|
|
|
* @param array $items the data item used to generate the checkboxes. |
941
|
|
|
* The array keys are the checkbox values, while the array values are the corresponding labels. |
942
|
|
|
* @param array $options options (name => config) for the checkbox list container tag. |
943
|
|
|
* The following options are specially handled: |
944
|
|
|
* |
945
|
|
|
* - tag: string|false, the tag name of the container element. False to render checkbox without container. |
946
|
|
|
* See also [[tag()]]. |
947
|
|
|
* - unselect: string, the value that should be submitted when none of the checkboxes is selected. |
948
|
|
|
* By setting this option, a hidden input will be generated. |
949
|
|
|
* - disabled: boolean, whether the generated by unselect option hidden input should be disabled. Defaults to false. |
950
|
|
|
* This option is available since version 2.0.16. |
951
|
|
|
* - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
952
|
|
|
* This option is ignored if `item` option is set. |
953
|
|
|
* - strict: boolean, if `$selection` is an array and this value is true a strict comparison will be performed on `$items` keys. Defaults to false. |
954
|
|
|
* This option is available since 2.0.37. |
955
|
|
|
* - separator: string, the HTML code that separates items. |
956
|
|
|
* - itemOptions: array, the options for generating the checkbox tag using [[checkbox()]]. |
957
|
|
|
* - item: callable, a callback that can be used to customize the generation of the HTML code |
958
|
|
|
* corresponding to a single item in $items. The signature of this callback must be: |
959
|
|
|
* |
960
|
|
|
* ```php |
961
|
|
|
* function ($index, $label, $name, $checked, $value) |
962
|
|
|
* ``` |
963
|
|
|
* |
964
|
|
|
* where $index is the zero-based index of the checkbox in the whole list; $label |
965
|
|
|
* is the label for the checkbox; and $name, $value and $checked represent the name, |
966
|
|
|
* value and the checked status of the checkbox input, respectively. |
967
|
|
|
* |
968
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
969
|
|
|
* |
970
|
|
|
* @return string the generated checkbox list |
971
|
|
|
*/ |
972
|
4 |
|
public static function checkboxList($name, $selection = null, $items = [], $options = []) |
973
|
|
|
{ |
974
|
4 |
|
if (substr($name, -2) !== '[]') { |
975
|
4 |
|
$name .= '[]'; |
976
|
|
|
} |
977
|
4 |
|
if (ArrayHelper::isTraversable($selection)) { |
978
|
2 |
|
$selection = array_map('strval', ArrayHelper::toArray($selection)); |
979
|
|
|
} |
980
|
|
|
|
981
|
4 |
|
$formatter = ArrayHelper::remove($options, 'item'); |
982
|
4 |
|
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []); |
983
|
4 |
|
$encode = ArrayHelper::remove($options, 'encode', true); |
984
|
4 |
|
$separator = ArrayHelper::remove($options, 'separator', "\n"); |
985
|
4 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
986
|
4 |
|
$strict = ArrayHelper::remove($options, 'strict', false); |
987
|
|
|
|
988
|
4 |
|
$lines = []; |
989
|
4 |
|
$index = 0; |
990
|
4 |
|
foreach ($items as $value => $label) { |
991
|
4 |
|
$checked = $selection !== null && |
992
|
4 |
|
(!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection) |
|
|
|
|
993
|
4 |
|
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection, $strict)); |
|
|
|
|
994
|
4 |
|
if ($formatter !== null) { |
995
|
1 |
|
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value); |
996
|
|
|
} else { |
997
|
4 |
|
$lines[] = static::checkbox($name, $checked, array_merge([ |
998
|
4 |
|
'value' => $value, |
999
|
4 |
|
'label' => $encode ? static::encode($label) : $label, |
1000
|
4 |
|
], $itemOptions)); |
|
|
|
|
1001
|
|
|
} |
1002
|
4 |
|
$index++; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
4 |
|
if (isset($options['unselect'])) { |
1006
|
|
|
// add a hidden field so that if the list box has no option being selected, it still submits a value |
1007
|
3 |
|
$name2 = substr($name, -2) === '[]' ? substr($name, 0, -2) : $name; |
1008
|
3 |
|
$hiddenOptions = []; |
1009
|
|
|
// make sure disabled input is not sending any value |
1010
|
3 |
|
if (!empty($options['disabled'])) { |
1011
|
1 |
|
$hiddenOptions['disabled'] = $options['disabled']; |
1012
|
|
|
} |
1013
|
3 |
|
$hidden = static::hiddenInput($name2, $options['unselect'], $hiddenOptions); |
1014
|
3 |
|
unset($options['unselect'], $options['disabled']); |
1015
|
|
|
} else { |
1016
|
2 |
|
$hidden = ''; |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
4 |
|
$visibleContent = implode($separator, $lines); |
|
|
|
|
1020
|
|
|
|
1021
|
4 |
|
if ($tag === false) { |
1022
|
1 |
|
return $hidden . $visibleContent; |
1023
|
|
|
} |
1024
|
|
|
|
1025
|
4 |
|
return $hidden . static::tag($tag, $visibleContent, $options); |
1026
|
|
|
} |
1027
|
|
|
|
1028
|
|
|
/** |
1029
|
|
|
* Generates a list of radio buttons. |
1030
|
|
|
* A radio button list is like a checkbox list, except that it only allows single selection. |
1031
|
|
|
* @param string $name the name attribute of each radio button. |
1032
|
|
|
* @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
1033
|
|
|
* @param array $items the data item used to generate the radio buttons. |
1034
|
|
|
* The array keys are the radio button values, while the array values are the corresponding labels. |
1035
|
|
|
* @param array $options options (name => config) for the radio button list container tag. |
1036
|
|
|
* The following options are specially handled: |
1037
|
|
|
* |
1038
|
|
|
* - tag: string|false, the tag name of the container element. False to render radio buttons without container. |
1039
|
|
|
* See also [[tag()]]. |
1040
|
|
|
* - unselect: string, the value that should be submitted when none of the radio buttons is selected. |
1041
|
|
|
* By setting this option, a hidden input will be generated. |
1042
|
|
|
* - disabled: boolean, whether the generated by unselect option hidden input should be disabled. Defaults to false. |
1043
|
|
|
* This option is available since version 2.0.16. |
1044
|
|
|
* - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
1045
|
|
|
* This option is ignored if `item` option is set. |
1046
|
|
|
* - strict: boolean, if `$selection` is an array and this value is true a strict comparison will be performed on `$items` keys. Defaults to false. |
1047
|
|
|
* This option is available since 2.0.37. |
1048
|
|
|
* - separator: string, the HTML code that separates items. |
1049
|
|
|
* - itemOptions: array, the options for generating the radio button tag using [[radio()]]. |
1050
|
|
|
* - item: callable, a callback that can be used to customize the generation of the HTML code |
1051
|
|
|
* corresponding to a single item in $items. The signature of this callback must be: |
1052
|
|
|
* |
1053
|
|
|
* ```php |
1054
|
|
|
* function ($index, $label, $name, $checked, $value) |
1055
|
|
|
* ``` |
1056
|
|
|
* |
1057
|
|
|
* where $index is the zero-based index of the radio button in the whole list; $label |
1058
|
|
|
* is the label for the radio button; and $name, $value and $checked represent the name, |
1059
|
|
|
* value and the checked status of the radio button input, respectively. |
1060
|
|
|
* |
1061
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1062
|
|
|
* |
1063
|
|
|
* @return string the generated radio button list |
1064
|
|
|
*/ |
1065
|
4 |
|
public static function radioList($name, $selection = null, $items = [], $options = []) |
1066
|
|
|
{ |
1067
|
4 |
|
if (ArrayHelper::isTraversable($selection)) { |
1068
|
2 |
|
$selection = array_map('strval', ArrayHelper::toArray($selection)); |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
4 |
|
$formatter = ArrayHelper::remove($options, 'item'); |
1072
|
4 |
|
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []); |
1073
|
4 |
|
$encode = ArrayHelper::remove($options, 'encode', true); |
1074
|
4 |
|
$separator = ArrayHelper::remove($options, 'separator', "\n"); |
1075
|
4 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
1076
|
4 |
|
$strict = ArrayHelper::remove($options, 'strict', false); |
1077
|
|
|
|
1078
|
4 |
|
$hidden = ''; |
1079
|
4 |
|
if (isset($options['unselect'])) { |
1080
|
|
|
// add a hidden field so that if the list box has no option being selected, it still submits a value |
1081
|
3 |
|
$hiddenOptions = []; |
1082
|
|
|
// make sure disabled input is not sending any value |
1083
|
3 |
|
if (!empty($options['disabled'])) { |
1084
|
1 |
|
$hiddenOptions['disabled'] = $options['disabled']; |
1085
|
|
|
} |
1086
|
3 |
|
$hidden = static::hiddenInput($name, $options['unselect'], $hiddenOptions); |
1087
|
3 |
|
unset($options['unselect'], $options['disabled']); |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
4 |
|
$lines = []; |
1091
|
4 |
|
$index = 0; |
1092
|
4 |
|
foreach ($items as $value => $label) { |
1093
|
4 |
|
$checked = $selection !== null && |
1094
|
4 |
|
(!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection) |
|
|
|
|
1095
|
4 |
|
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection, $strict)); |
|
|
|
|
1096
|
4 |
|
if ($formatter !== null) { |
1097
|
1 |
|
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value); |
1098
|
|
|
} else { |
1099
|
4 |
|
$lines[] = static::radio($name, $checked, array_merge([ |
1100
|
4 |
|
'value' => $value, |
1101
|
4 |
|
'label' => $encode ? static::encode($label) : $label, |
1102
|
4 |
|
], $itemOptions)); |
|
|
|
|
1103
|
|
|
} |
1104
|
4 |
|
$index++; |
1105
|
|
|
} |
1106
|
4 |
|
$visibleContent = implode($separator, $lines); |
|
|
|
|
1107
|
|
|
|
1108
|
4 |
|
if ($tag === false) { |
1109
|
1 |
|
return $hidden . $visibleContent; |
1110
|
|
|
} |
1111
|
|
|
|
1112
|
4 |
|
return $hidden . static::tag($tag, $visibleContent, $options); |
1113
|
|
|
} |
1114
|
|
|
|
1115
|
|
|
/** |
1116
|
|
|
* Generates an unordered list. |
1117
|
|
|
* @param array|\Traversable $items the items for generating the list. Each item generates a single list item. |
1118
|
|
|
* Note that items will be automatically HTML encoded if `$options['encode']` is not set or true. |
1119
|
|
|
* @param array $options options (name => config) for the radio button list. The following options are supported: |
1120
|
|
|
* |
1121
|
|
|
* - encode: boolean, whether to HTML-encode the items. Defaults to true. |
1122
|
|
|
* This option is ignored if the `item` option is specified. |
1123
|
|
|
* - separator: string, the HTML code that separates items. Defaults to a simple newline (`"\n"`). |
1124
|
|
|
* This option is available since version 2.0.7. |
1125
|
|
|
* - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified. |
1126
|
|
|
* - item: callable, a callback that is used to generate each individual list item. |
1127
|
|
|
* The signature of this callback must be: |
1128
|
|
|
* |
1129
|
|
|
* ```php |
1130
|
|
|
* function ($item, $index) |
1131
|
|
|
* ``` |
1132
|
|
|
* |
1133
|
|
|
* where $index is the array key corresponding to `$item` in `$items`. The callback should return |
1134
|
|
|
* the whole list item tag. |
1135
|
|
|
* |
1136
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1137
|
|
|
* |
1138
|
|
|
* @return string the generated unordered list. An empty list tag will be returned if `$items` is empty. |
1139
|
|
|
*/ |
1140
|
5 |
|
public static function ul($items, $options = []) |
1141
|
|
|
{ |
1142
|
5 |
|
$tag = ArrayHelper::remove($options, 'tag', 'ul'); |
1143
|
5 |
|
$encode = ArrayHelper::remove($options, 'encode', true); |
1144
|
5 |
|
$formatter = ArrayHelper::remove($options, 'item'); |
1145
|
5 |
|
$separator = ArrayHelper::remove($options, 'separator', "\n"); |
1146
|
5 |
|
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []); |
1147
|
|
|
|
1148
|
5 |
|
if (empty($items)) { |
1149
|
2 |
|
return static::tag($tag, '', $options); |
1150
|
|
|
} |
1151
|
|
|
|
1152
|
5 |
|
$results = []; |
1153
|
5 |
|
foreach ($items as $index => $item) { |
1154
|
5 |
|
if ($formatter !== null) { |
1155
|
2 |
|
$results[] = call_user_func($formatter, $item, $index); |
1156
|
|
|
} else { |
1157
|
5 |
|
$results[] = static::tag('li', $encode ? static::encode($item) : $item, $itemOptions); |
1158
|
|
|
} |
1159
|
|
|
} |
1160
|
|
|
|
1161
|
5 |
|
return static::tag( |
1162
|
5 |
|
$tag, |
1163
|
5 |
|
$separator . implode($separator, $results) . $separator, |
|
|
|
|
1164
|
5 |
|
$options |
1165
|
5 |
|
); |
1166
|
|
|
} |
1167
|
|
|
|
1168
|
|
|
/** |
1169
|
|
|
* Generates an ordered list. |
1170
|
|
|
* @param array|\Traversable $items the items for generating the list. Each item generates a single list item. |
1171
|
|
|
* Note that items will be automatically HTML encoded if `$options['encode']` is not set or true. |
1172
|
|
|
* @param array $options options (name => config) for the radio button list. The following options are supported: |
1173
|
|
|
* |
1174
|
|
|
* - encode: boolean, whether to HTML-encode the items. Defaults to true. |
1175
|
|
|
* This option is ignored if the `item` option is specified. |
1176
|
|
|
* - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified. |
1177
|
|
|
* - item: callable, a callback that is used to generate each individual list item. |
1178
|
|
|
* The signature of this callback must be: |
1179
|
|
|
* |
1180
|
|
|
* ```php |
1181
|
|
|
* function ($item, $index) |
1182
|
|
|
* ``` |
1183
|
|
|
* |
1184
|
|
|
* where $index is the array key corresponding to `$item` in `$items`. The callback should return |
1185
|
|
|
* the whole list item tag. |
1186
|
|
|
* |
1187
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1188
|
|
|
* |
1189
|
|
|
* @return string the generated ordered list. An empty string is returned if `$items` is empty. |
1190
|
|
|
*/ |
1191
|
1 |
|
public static function ol($items, $options = []) |
1192
|
|
|
{ |
1193
|
1 |
|
$options['tag'] = 'ol'; |
1194
|
1 |
|
return static::ul($items, $options); |
1195
|
|
|
} |
1196
|
|
|
|
1197
|
|
|
/** |
1198
|
|
|
* Generates a label tag for the given model attribute. |
1199
|
|
|
* The label text is the label associated with the attribute, obtained via [[Model::getAttributeLabel()]]. |
1200
|
|
|
* @param Model $model the model object |
1201
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1202
|
|
|
* about attribute expression. |
1203
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1204
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1205
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
1206
|
|
|
* The following options are specially handled: |
1207
|
|
|
* |
1208
|
|
|
* - label: this specifies the label to be displayed. Note that this will NOT be [[encode()|encoded]]. |
1209
|
|
|
* If this is not set, [[Model::getAttributeLabel()]] will be called to get the label for display |
1210
|
|
|
* (after encoding). |
1211
|
|
|
* |
1212
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1213
|
|
|
* |
1214
|
|
|
* @return string the generated label tag |
1215
|
|
|
*/ |
1216
|
17 |
|
public static function activeLabel($model, $attribute, $options = []) |
1217
|
|
|
{ |
1218
|
17 |
|
$for = ArrayHelper::remove($options, 'for', static::getInputId($model, $attribute)); |
1219
|
17 |
|
$attribute = static::getAttributeName($attribute); |
1220
|
17 |
|
$label = ArrayHelper::remove($options, 'label', static::encode($model->getAttributeLabel($attribute))); |
1221
|
17 |
|
return static::label($label, $for, $options); |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
|
|
/** |
1225
|
|
|
* Generates a hint tag for the given model attribute. |
1226
|
|
|
* The hint text is the hint associated with the attribute, obtained via [[Model::getAttributeHint()]]. |
1227
|
|
|
* If no hint content can be obtained, method will return an empty string. |
1228
|
|
|
* @param Model $model the model object |
1229
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1230
|
|
|
* about attribute expression. |
1231
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1232
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1233
|
|
|
* If a value is null, the corresponding attribute will not be rendered. |
1234
|
|
|
* The following options are specially handled: |
1235
|
|
|
* |
1236
|
|
|
* - hint: this specifies the hint to be displayed. Note that this will NOT be [[encode()|encoded]]. |
1237
|
|
|
* If this is not set, [[Model::getAttributeHint()]] will be called to get the hint for display |
1238
|
|
|
* (without encoding). |
1239
|
|
|
* |
1240
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1241
|
|
|
* |
1242
|
|
|
* @return string the generated hint tag |
1243
|
|
|
* @since 2.0.4 |
1244
|
|
|
*/ |
1245
|
17 |
|
public static function activeHint($model, $attribute, $options = []) |
1246
|
|
|
{ |
1247
|
17 |
|
$attribute = static::getAttributeName($attribute); |
1248
|
17 |
|
$hint = isset($options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); |
1249
|
17 |
|
if (empty($hint)) { |
1250
|
4 |
|
return ''; |
1251
|
|
|
} |
1252
|
13 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
1253
|
13 |
|
unset($options['hint']); |
1254
|
13 |
|
return static::tag($tag, $hint, $options); |
1255
|
|
|
} |
1256
|
|
|
|
1257
|
|
|
/** |
1258
|
|
|
* Generates a summary of the validation errors. |
1259
|
|
|
* If there is no validation error, an empty error summary markup will still be generated, but it will be hidden. |
1260
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
1261
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
1262
|
|
|
* |
1263
|
|
|
* - header: string, the header HTML for the error summary. If not set, a default prompt string will be used. |
1264
|
|
|
* - footer: string, the footer HTML for the error summary. Defaults to empty string. |
1265
|
|
|
* - encode: boolean, if set to false then the error messages won't be encoded. Defaults to `true`. |
1266
|
|
|
* - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise |
1267
|
|
|
* only the first error message for each attribute will be shown. Defaults to `false`. |
1268
|
|
|
* Option is available since 2.0.10. |
1269
|
|
|
* - emptyClass: string, the class name that is added to an empty summary. |
1270
|
|
|
* |
1271
|
|
|
* The rest of the options will be rendered as the attributes of the container tag. |
1272
|
|
|
* |
1273
|
|
|
* @return string the generated error summary |
1274
|
|
|
*/ |
1275
|
9 |
|
public static function errorSummary($models, $options = []) |
1276
|
|
|
{ |
1277
|
9 |
|
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>'; |
1278
|
9 |
|
$footer = ArrayHelper::remove($options, 'footer', ''); |
1279
|
9 |
|
$encode = ArrayHelper::remove($options, 'encode', true); |
1280
|
9 |
|
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false); |
1281
|
9 |
|
$emptyClass = ArrayHelper::remove($options, 'emptyClass', null); |
1282
|
9 |
|
unset($options['header']); |
1283
|
9 |
|
$lines = self::collectErrors($models, $encode, $showAllErrors); |
1284
|
9 |
|
if (empty($lines)) { |
1285
|
|
|
// still render the placeholder for client-side validation use |
1286
|
3 |
|
$content = '<ul></ul>'; |
1287
|
3 |
|
if ($emptyClass !== null) { |
1288
|
1 |
|
$options['class'] = $emptyClass; |
1289
|
|
|
} else { |
1290
|
3 |
|
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none'; |
1291
|
|
|
} |
1292
|
|
|
} else { |
1293
|
6 |
|
$content = '<ul><li>' . implode("</li>\n<li>", $lines) . '</li></ul>'; |
1294
|
|
|
} |
1295
|
|
|
|
1296
|
9 |
|
return Html::tag('div', $header . $content . $footer, $options); |
1297
|
|
|
} |
1298
|
|
|
|
1299
|
|
|
/** |
1300
|
|
|
* Return array of the validation errors |
1301
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
1302
|
|
|
* @param $encode boolean, if set to false then the error messages won't be encoded. |
1303
|
|
|
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise |
1304
|
|
|
* only the first error message for each attribute will be shown. |
1305
|
|
|
* @return array of the validation errors |
1306
|
|
|
* @since 2.0.14 |
1307
|
|
|
*/ |
1308
|
9 |
|
private static function collectErrors($models, $encode, $showAllErrors) |
1309
|
|
|
{ |
1310
|
9 |
|
$lines = []; |
1311
|
9 |
|
if (!is_array($models)) { |
1312
|
9 |
|
$models = [$models]; |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
9 |
|
foreach ($models as $model) { |
1316
|
9 |
|
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors))); |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
|
|
// If there are the same error messages for different attributes, array_unique will leave gaps |
1320
|
|
|
// between sequential keys. Applying array_values to reorder array keys. |
1321
|
9 |
|
$lines = array_values($lines); |
1322
|
|
|
|
1323
|
9 |
|
if ($encode) { |
1324
|
8 |
|
foreach ($lines as &$line) { |
1325
|
5 |
|
$line = Html::encode($line); |
1326
|
|
|
} |
1327
|
|
|
} |
1328
|
|
|
|
1329
|
9 |
|
return $lines; |
1330
|
|
|
} |
1331
|
|
|
|
1332
|
|
|
/** |
1333
|
|
|
* Generates a tag that contains the first validation error of the specified model attribute. |
1334
|
|
|
* Note that even if there is no validation error, this method will still return an empty error tag. |
1335
|
|
|
* @param Model $model the model object |
1336
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1337
|
|
|
* about attribute expression. |
1338
|
|
|
* @param array $options the tag options in terms of name-value pairs. The values will be HTML-encoded |
1339
|
|
|
* using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
1340
|
|
|
* |
1341
|
|
|
* The following options are specially handled: |
1342
|
|
|
* |
1343
|
|
|
* - tag: this specifies the tag name. If not set, "div" will be used. |
1344
|
|
|
* See also [[tag()]]. |
1345
|
|
|
* - encode: boolean, if set to false then the error message won't be encoded. |
1346
|
|
|
* - errorSource (since 2.0.14): \Closure|callable, callback that will be called to obtain an error message. |
1347
|
|
|
* The signature of the callback must be: `function ($model, $attribute)` and return a string. |
1348
|
|
|
* When not set, the `$model->getFirstError()` method will be called. |
1349
|
|
|
* |
1350
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1351
|
|
|
* |
1352
|
|
|
* @return string the generated label tag |
1353
|
|
|
*/ |
1354
|
16 |
|
public static function error($model, $attribute, $options = []) |
1355
|
|
|
{ |
1356
|
16 |
|
$attribute = static::getAttributeName($attribute); |
1357
|
16 |
|
$errorSource = ArrayHelper::remove($options, 'errorSource'); |
1358
|
16 |
|
if ($errorSource !== null) { |
1359
|
1 |
|
$error = call_user_func($errorSource, $model, $attribute); |
1360
|
|
|
} else { |
1361
|
16 |
|
$error = $model->getFirstError($attribute); |
1362
|
|
|
} |
1363
|
16 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
1364
|
16 |
|
$encode = ArrayHelper::remove($options, 'encode', true); |
1365
|
16 |
|
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options); |
1366
|
|
|
} |
1367
|
|
|
|
1368
|
|
|
/** |
1369
|
|
|
* Generates an input tag for the given model attribute. |
1370
|
|
|
* This method will generate the "name" and "value" tag attributes automatically for the model attribute |
1371
|
|
|
* unless they are explicitly specified in `$options`. |
1372
|
|
|
* @param string $type the input type (e.g. 'text', 'password') |
1373
|
|
|
* @param Model $model the model object |
1374
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1375
|
|
|
* about attribute expression. |
1376
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1377
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1378
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1379
|
|
|
* @return string the generated input tag |
1380
|
|
|
*/ |
1381
|
38 |
|
public static function activeInput($type, $model, $attribute, $options = []) |
1382
|
|
|
{ |
1383
|
38 |
|
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); |
1384
|
38 |
|
$value = isset($options['value']) ? $options['value'] : static::getAttributeValue($model, $attribute); |
1385
|
38 |
|
if (!array_key_exists('id', $options)) { |
1386
|
34 |
|
$options['id'] = static::getInputId($model, $attribute); |
1387
|
|
|
} |
1388
|
|
|
|
1389
|
38 |
|
static::setActivePlaceholder($model, $attribute, $options); |
1390
|
38 |
|
self::normalizeMaxLength($model, $attribute, $options); |
1391
|
|
|
|
1392
|
38 |
|
return static::input($type, $name, $value, $options); |
|
|
|
|
1393
|
|
|
} |
1394
|
|
|
|
1395
|
|
|
/** |
1396
|
|
|
* If `maxlength` option is set true and the model attribute is validated by a string validator, |
1397
|
|
|
* the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] and |
1398
|
|
|
* [[\yii\validators\StringValidator::length]]. |
1399
|
|
|
* @param Model $model the model object |
1400
|
|
|
* @param string $attribute the attribute name or expression. |
1401
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
1402
|
|
|
*/ |
1403
|
42 |
|
private static function normalizeMaxLength($model, $attribute, &$options) |
1404
|
|
|
{ |
1405
|
42 |
|
if (isset($options['maxlength']) && $options['maxlength'] === true) { |
1406
|
7 |
|
unset($options['maxlength']); |
1407
|
7 |
|
$attrName = static::getAttributeName($attribute); |
1408
|
7 |
|
foreach ($model->getActiveValidators($attrName) as $validator) { |
1409
|
7 |
|
if ($validator instanceof StringValidator && ($validator->max !== null || $validator->length !== null)) { |
1410
|
6 |
|
$options['maxlength'] = max($validator->max, $validator->length); |
1411
|
6 |
|
break; |
1412
|
|
|
} |
1413
|
|
|
} |
1414
|
|
|
} |
1415
|
|
|
} |
1416
|
|
|
|
1417
|
|
|
/** |
1418
|
|
|
* Generates a text input tag for the given model attribute. |
1419
|
|
|
* This method will generate the "name" and "value" tag attributes automatically for the model attribute |
1420
|
|
|
* unless they are explicitly specified in `$options`. |
1421
|
|
|
* @param Model $model the model object |
1422
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1423
|
|
|
* about attribute expression. |
1424
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1425
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1426
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1427
|
|
|
* The following special options are recognized: |
1428
|
|
|
* |
1429
|
|
|
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
1430
|
|
|
* by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] |
1431
|
|
|
* and [[\yii\validators\StringValidator::length]. |
1432
|
|
|
* This is available since version 2.0.3 and improved taking `length` into account since version 2.0.42. |
1433
|
|
|
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used |
1434
|
|
|
* as a placeholder (this behavior is available since version 2.0.14). |
1435
|
|
|
* |
1436
|
|
|
* @return string the generated input tag |
1437
|
|
|
*/ |
1438
|
22 |
|
public static function activeTextInput($model, $attribute, $options = []) |
1439
|
|
|
{ |
1440
|
22 |
|
return static::activeInput('text', $model, $attribute, $options); |
1441
|
|
|
} |
1442
|
|
|
|
1443
|
|
|
/** |
1444
|
|
|
* Generate placeholder from model attribute label. |
1445
|
|
|
* |
1446
|
|
|
* @param Model $model the model object |
1447
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1448
|
|
|
* about attribute expression. |
1449
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1450
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1451
|
|
|
* @since 2.0.14 |
1452
|
|
|
*/ |
1453
|
41 |
|
protected static function setActivePlaceholder($model, $attribute, &$options = []) |
1454
|
|
|
{ |
1455
|
41 |
|
if (isset($options['placeholder']) && $options['placeholder'] === true) { |
1456
|
2 |
|
$attribute = static::getAttributeName($attribute); |
1457
|
2 |
|
$options['placeholder'] = $model->getAttributeLabel($attribute); |
1458
|
|
|
} |
1459
|
|
|
} |
1460
|
|
|
|
1461
|
|
|
/** |
1462
|
|
|
* Generates a hidden input tag for the given model attribute. |
1463
|
|
|
* This method will generate the "name" and "value" tag attributes automatically for the model attribute |
1464
|
|
|
* unless they are explicitly specified in `$options`. |
1465
|
|
|
* @param Model $model the model object |
1466
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1467
|
|
|
* about attribute expression. |
1468
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1469
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1470
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1471
|
|
|
* @return string the generated input tag |
1472
|
|
|
*/ |
1473
|
5 |
|
public static function activeHiddenInput($model, $attribute, $options = []) |
1474
|
|
|
{ |
1475
|
5 |
|
return static::activeInput('hidden', $model, $attribute, $options); |
1476
|
|
|
} |
1477
|
|
|
|
1478
|
|
|
/** |
1479
|
|
|
* Generates a password input tag for the given model attribute. |
1480
|
|
|
* This method will generate the "name" and "value" tag attributes automatically for the model attribute |
1481
|
|
|
* unless they are explicitly specified in `$options`. |
1482
|
|
|
* @param Model $model the model object |
1483
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1484
|
|
|
* about attribute expression. |
1485
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1486
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1487
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1488
|
|
|
* The following special options are recognized: |
1489
|
|
|
* |
1490
|
|
|
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
1491
|
|
|
* by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] |
1492
|
|
|
* and [[\yii\validators\StringValidator::length]. |
1493
|
|
|
* This is available since version 2.0.6 and improved taking `length` into account since version 2.0.42. |
1494
|
|
|
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used |
1495
|
|
|
* as a placeholder (this behavior is available since version 2.0.14). |
1496
|
|
|
* |
1497
|
|
|
* @return string the generated input tag |
1498
|
|
|
*/ |
1499
|
3 |
|
public static function activePasswordInput($model, $attribute, $options = []) |
1500
|
|
|
{ |
1501
|
3 |
|
return static::activeInput('password', $model, $attribute, $options); |
1502
|
|
|
} |
1503
|
|
|
|
1504
|
|
|
/** |
1505
|
|
|
* Generates a file input tag for the given model attribute. |
1506
|
|
|
* This method will generate the "name" and "value" tag attributes automatically for the model attribute |
1507
|
|
|
* unless they are explicitly specified in `$options`. |
1508
|
|
|
* Additionally, if a separate set of HTML options array is defined inside `$options` with a key named `hiddenOptions`, |
1509
|
|
|
* it will be passed to the `activeHiddenInput` field as its own `$options` parameter. |
1510
|
|
|
* @param Model $model the model object |
1511
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1512
|
|
|
* about attribute expression. |
1513
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1514
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1515
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1516
|
|
|
* If `hiddenOptions` parameter which is another set of HTML options array is defined, it will be extracted |
1517
|
|
|
* from `$options` to be used for the hidden input. |
1518
|
|
|
* @return string the generated input tag |
1519
|
|
|
*/ |
1520
|
2 |
|
public static function activeFileInput($model, $attribute, $options = []) |
1521
|
|
|
{ |
1522
|
2 |
|
$hiddenOptions = ['id' => null, 'value' => '']; |
1523
|
2 |
|
if (isset($options['name'])) { |
1524
|
1 |
|
$hiddenOptions['name'] = $options['name']; |
1525
|
|
|
} |
1526
|
|
|
// make sure disabled input is not sending any value |
1527
|
2 |
|
if (!empty($options['disabled'])) { |
1528
|
1 |
|
$hiddenOptions['disabled'] = $options['disabled']; |
1529
|
|
|
} |
1530
|
2 |
|
$hiddenOptions = ArrayHelper::merge($hiddenOptions, ArrayHelper::remove($options, 'hiddenOptions', [])); |
1531
|
|
|
// Add a hidden field so that if a model only has a file field, we can |
1532
|
|
|
// still use isset($_POST[$modelClass]) to detect if the input is submitted. |
1533
|
|
|
// The hidden input will be assigned its own set of html options via `$hiddenOptions`. |
1534
|
|
|
// This provides the possibility to interact with the hidden field via client script. |
1535
|
|
|
// Note: For file-field-only model with `disabled` option set to `true` input submitting detection won't work. |
1536
|
|
|
|
1537
|
2 |
|
return static::activeHiddenInput($model, $attribute, $hiddenOptions) |
1538
|
2 |
|
. static::activeInput('file', $model, $attribute, $options); |
1539
|
|
|
} |
1540
|
|
|
|
1541
|
|
|
/** |
1542
|
|
|
* Generates a textarea tag for the given model attribute. |
1543
|
|
|
* The model attribute value will be used as the content in the textarea. |
1544
|
|
|
* @param Model $model the model object |
1545
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1546
|
|
|
* about attribute expression. |
1547
|
|
|
* @param array $options the tag options in terms of name-value pairs. These will be rendered as |
1548
|
|
|
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
1549
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1550
|
|
|
* The following special options are recognized: |
1551
|
|
|
* |
1552
|
|
|
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
1553
|
|
|
* by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] |
1554
|
|
|
* and [[\yii\validators\StringValidator::length]. |
1555
|
|
|
* This is available since version 2.0.6 and improved taking `length` into account since version 2.0.42. |
1556
|
|
|
* - placeholder: string|boolean, when `placeholder` equals `true`, the attribute label from the $model will be used |
1557
|
|
|
* as a placeholder (this behavior is available since version 2.0.14). |
1558
|
|
|
* |
1559
|
|
|
* @return string the generated textarea tag |
1560
|
|
|
*/ |
1561
|
4 |
|
public static function activeTextarea($model, $attribute, $options = []) |
1562
|
|
|
{ |
1563
|
4 |
|
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); |
1564
|
4 |
|
if (isset($options['value'])) { |
1565
|
1 |
|
$value = $options['value']; |
1566
|
1 |
|
unset($options['value']); |
1567
|
|
|
} else { |
1568
|
3 |
|
$value = static::getAttributeValue($model, $attribute); |
1569
|
|
|
} |
1570
|
4 |
|
if (!array_key_exists('id', $options)) { |
1571
|
4 |
|
$options['id'] = static::getInputId($model, $attribute); |
1572
|
|
|
} |
1573
|
4 |
|
self::normalizeMaxLength($model, $attribute, $options); |
1574
|
4 |
|
static::setActivePlaceholder($model, $attribute, $options); |
1575
|
4 |
|
return static::textarea($name, $value, $options); |
1576
|
|
|
} |
1577
|
|
|
|
1578
|
|
|
/** |
1579
|
|
|
* Generates a radio button tag together with a label for the given model attribute. |
1580
|
|
|
* This method will generate the "checked" tag attribute according to the model attribute value. |
1581
|
|
|
* @param Model $model the model object |
1582
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1583
|
|
|
* about attribute expression. |
1584
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
1585
|
|
|
* See [[booleanInput()]] for details about accepted attributes. |
1586
|
|
|
* |
1587
|
|
|
* @return string the generated radio button tag |
1588
|
|
|
*/ |
1589
|
5 |
|
public static function activeRadio($model, $attribute, $options = []) |
1590
|
|
|
{ |
1591
|
5 |
|
return static::activeBooleanInput('radio', $model, $attribute, $options); |
1592
|
|
|
} |
1593
|
|
|
|
1594
|
|
|
/** |
1595
|
|
|
* Generates a checkbox tag together with a label for the given model attribute. |
1596
|
|
|
* This method will generate the "checked" tag attribute according to the model attribute value. |
1597
|
|
|
* @param Model $model the model object |
1598
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1599
|
|
|
* about attribute expression. |
1600
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
1601
|
|
|
* See [[booleanInput()]] for details about accepted attributes. |
1602
|
|
|
* |
1603
|
|
|
* @return string the generated checkbox tag |
1604
|
|
|
*/ |
1605
|
5 |
|
public static function activeCheckbox($model, $attribute, $options = []) |
1606
|
|
|
{ |
1607
|
5 |
|
return static::activeBooleanInput('checkbox', $model, $attribute, $options); |
1608
|
|
|
} |
1609
|
|
|
|
1610
|
|
|
/** |
1611
|
|
|
* Generates a boolean input |
1612
|
|
|
* This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]]. |
1613
|
|
|
* @param string $type the input type. This can be either `radio` or `checkbox`. |
1614
|
|
|
* @param Model $model the model object |
1615
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1616
|
|
|
* about attribute expression. |
1617
|
|
|
* @param array $options the tag options in terms of name-value pairs. |
1618
|
|
|
* See [[booleanInput()]] for details about accepted attributes. |
1619
|
|
|
* @return string the generated input element |
1620
|
|
|
* @since 2.0.9 |
1621
|
|
|
*/ |
1622
|
9 |
|
protected static function activeBooleanInput($type, $model, $attribute, $options = []) |
1623
|
|
|
{ |
1624
|
9 |
|
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); |
1625
|
9 |
|
$value = static::getAttributeValue($model, $attribute); |
1626
|
|
|
|
1627
|
9 |
|
if (!array_key_exists('value', $options)) { |
1628
|
9 |
|
$options['value'] = '1'; |
1629
|
|
|
} |
1630
|
9 |
|
if (!array_key_exists('uncheck', $options)) { |
1631
|
5 |
|
$options['uncheck'] = '0'; |
1632
|
4 |
|
} elseif ($options['uncheck'] === false) { |
1633
|
4 |
|
unset($options['uncheck']); |
1634
|
|
|
} |
1635
|
9 |
|
if (!array_key_exists('label', $options)) { |
1636
|
5 |
|
$options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute))); |
1637
|
4 |
|
} elseif ($options['label'] === false) { |
1638
|
4 |
|
unset($options['label']); |
1639
|
|
|
} |
1640
|
|
|
|
1641
|
9 |
|
$checked = "$value" === "{$options['value']}"; |
1642
|
|
|
|
1643
|
9 |
|
if (!array_key_exists('id', $options)) { |
1644
|
9 |
|
$options['id'] = static::getInputId($model, $attribute); |
1645
|
|
|
} |
1646
|
|
|
|
1647
|
9 |
|
return static::$type($name, $checked, $options); |
1648
|
|
|
} |
1649
|
|
|
|
1650
|
|
|
/** |
1651
|
|
|
* Generates a drop-down list for the given model attribute. |
1652
|
|
|
* The selection of the drop-down list is taken from the value of the model attribute. |
1653
|
|
|
* @param Model $model the model object |
1654
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1655
|
|
|
* about attribute expression. |
1656
|
|
|
* @param array $items the option data items. The array keys are option values, and the array values |
1657
|
|
|
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
1658
|
|
|
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
1659
|
|
|
* If you have a list of data models, you may convert them into the format described above using |
1660
|
|
|
* [[\yii\helpers\ArrayHelper::map()]]. |
1661
|
|
|
* |
1662
|
|
|
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
1663
|
|
|
* the labels will also be HTML-encoded. |
1664
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
1665
|
|
|
* |
1666
|
|
|
* - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
1667
|
|
|
* to override the value and to set other tag attributes: |
1668
|
|
|
* |
1669
|
|
|
* ```php |
1670
|
|
|
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
1671
|
|
|
* ``` |
1672
|
|
|
* |
1673
|
|
|
* - options: array, the attributes for the select option tags. The array keys must be valid option values, |
1674
|
|
|
* and the array values are the extra attributes for the corresponding option tags. For example, |
1675
|
|
|
* |
1676
|
|
|
* ```php |
1677
|
|
|
* [ |
1678
|
|
|
* 'value1' => ['disabled' => true], |
1679
|
|
|
* 'value2' => ['label' => 'value 2'], |
1680
|
|
|
* ]; |
1681
|
|
|
* ``` |
1682
|
|
|
* |
1683
|
|
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
1684
|
|
|
* except that the array keys represent the optgroup labels specified in $items. |
1685
|
|
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
1686
|
|
|
* Defaults to false. |
1687
|
|
|
* - encode: bool, whether to encode option prompt and option value characters. |
1688
|
|
|
* Defaults to `true`. This option is available since 2.0.3. |
1689
|
|
|
* |
1690
|
|
|
* The rest of the options will be rendered as the attributes of the resulting tag. The values will |
1691
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
1692
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1693
|
|
|
* |
1694
|
|
|
* @return string the generated drop-down list tag |
1695
|
|
|
*/ |
1696
|
3 |
|
public static function activeDropDownList($model, $attribute, $items, $options = []) |
1697
|
|
|
{ |
1698
|
3 |
|
if (empty($options['multiple'])) { |
1699
|
2 |
|
return static::activeListInput('dropDownList', $model, $attribute, $items, $options); |
1700
|
|
|
} |
1701
|
|
|
|
1702
|
1 |
|
return static::activeListBox($model, $attribute, $items, $options); |
1703
|
|
|
} |
1704
|
|
|
|
1705
|
|
|
/** |
1706
|
|
|
* Generates a list box. |
1707
|
|
|
* The selection of the list box is taken from the value of the model attribute. |
1708
|
|
|
* @param Model $model the model object |
1709
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1710
|
|
|
* about attribute expression. |
1711
|
|
|
* @param array $items the option data items. The array keys are option values, and the array values |
1712
|
|
|
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
1713
|
|
|
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
1714
|
|
|
* If you have a list of data models, you may convert them into the format described above using |
1715
|
|
|
* [[\yii\helpers\ArrayHelper::map()]]. |
1716
|
|
|
* |
1717
|
|
|
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
1718
|
|
|
* the labels will also be HTML-encoded. |
1719
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
1720
|
|
|
* |
1721
|
|
|
* - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
1722
|
|
|
* to override the value and to set other tag attributes: |
1723
|
|
|
* |
1724
|
|
|
* ```php |
1725
|
|
|
* ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
1726
|
|
|
* ``` |
1727
|
|
|
* |
1728
|
|
|
* - options: array, the attributes for the select option tags. The array keys must be valid option values, |
1729
|
|
|
* and the array values are the extra attributes for the corresponding option tags. For example, |
1730
|
|
|
* |
1731
|
|
|
* ```php |
1732
|
|
|
* [ |
1733
|
|
|
* 'value1' => ['disabled' => true], |
1734
|
|
|
* 'value2' => ['label' => 'value 2'], |
1735
|
|
|
* ]; |
1736
|
|
|
* ``` |
1737
|
|
|
* |
1738
|
|
|
* - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
1739
|
|
|
* except that the array keys represent the optgroup labels specified in $items. |
1740
|
|
|
* - unselect: string, the value that will be submitted when no option is selected. |
1741
|
|
|
* When this attribute is set, a hidden field will be generated so that if no option is selected in multiple |
1742
|
|
|
* mode, we can still obtain the posted unselect value. |
1743
|
|
|
* - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
1744
|
|
|
* Defaults to false. |
1745
|
|
|
* - encode: bool, whether to encode option prompt and option value characters. |
1746
|
|
|
* Defaults to `true`. This option is available since 2.0.3. |
1747
|
|
|
* |
1748
|
|
|
* The rest of the options will be rendered as the attributes of the resulting tag. The values will |
1749
|
|
|
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
1750
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1751
|
|
|
* |
1752
|
|
|
* @return string the generated list box tag |
1753
|
|
|
*/ |
1754
|
3 |
|
public static function activeListBox($model, $attribute, $items, $options = []) |
1755
|
|
|
{ |
1756
|
3 |
|
return static::activeListInput('listBox', $model, $attribute, $items, $options); |
1757
|
|
|
} |
1758
|
|
|
|
1759
|
|
|
/** |
1760
|
|
|
* Generates a list of checkboxes. |
1761
|
|
|
* A checkbox list allows multiple selection, like [[listBox()]]. |
1762
|
|
|
* As a result, the corresponding submitted value is an array. |
1763
|
|
|
* The selection of the checkbox list is taken from the value of the model attribute. |
1764
|
|
|
* @param Model $model the model object |
1765
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1766
|
|
|
* about attribute expression. |
1767
|
|
|
* @param array $items the data item used to generate the checkboxes. |
1768
|
|
|
* The array keys are the checkbox values, and the array values are the corresponding labels. |
1769
|
|
|
* @param array $options options (name => config) for the checkbox list container tag. |
1770
|
|
|
* The following options are specially handled: |
1771
|
|
|
* |
1772
|
|
|
* - tag: string|false, the tag name of the container element. False to render checkbox without container. |
1773
|
|
|
* See also [[tag()]]. |
1774
|
|
|
* - unselect: string, the value that should be submitted when none of the checkboxes is selected. |
1775
|
|
|
* You may set this option to be null to prevent default value submission. |
1776
|
|
|
* If this option is not set, an empty string will be submitted. |
1777
|
|
|
* - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
1778
|
|
|
* This option is ignored if `item` option is set. |
1779
|
|
|
* - separator: string, the HTML code that separates items. |
1780
|
|
|
* - itemOptions: array, the options for generating the checkbox tag using [[checkbox()]]. |
1781
|
|
|
* - item: callable, a callback that can be used to customize the generation of the HTML code |
1782
|
|
|
* corresponding to a single item in $items. The signature of this callback must be: |
1783
|
|
|
* |
1784
|
|
|
* ```php |
1785
|
|
|
* function ($index, $label, $name, $checked, $value) |
1786
|
|
|
* ``` |
1787
|
|
|
* |
1788
|
|
|
* where $index is the zero-based index of the checkbox in the whole list; $label |
1789
|
|
|
* is the label for the checkbox; and $name, $value and $checked represent the name, |
1790
|
|
|
* value and the checked status of the checkbox input. |
1791
|
|
|
* |
1792
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1793
|
|
|
* |
1794
|
|
|
* @return string the generated checkbox list |
1795
|
|
|
*/ |
1796
|
2 |
|
public static function activeCheckboxList($model, $attribute, $items, $options = []) |
1797
|
|
|
{ |
1798
|
2 |
|
return static::activeListInput('checkboxList', $model, $attribute, $items, $options); |
1799
|
|
|
} |
1800
|
|
|
|
1801
|
|
|
/** |
1802
|
|
|
* Generates a list of radio buttons. |
1803
|
|
|
* A radio button list is like a checkbox list, except that it only allows single selection. |
1804
|
|
|
* The selection of the radio buttons is taken from the value of the model attribute. |
1805
|
|
|
* @param Model $model the model object |
1806
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1807
|
|
|
* about attribute expression. |
1808
|
|
|
* @param array $items the data item used to generate the radio buttons. |
1809
|
|
|
* The array keys are the radio values, and the array values are the corresponding labels. |
1810
|
|
|
* @param array $options options (name => config) for the radio button list container tag. |
1811
|
|
|
* The following options are specially handled: |
1812
|
|
|
* |
1813
|
|
|
* - tag: string|false, the tag name of the container element. False to render radio button without container. |
1814
|
|
|
* See also [[tag()]]. |
1815
|
|
|
* - unselect: string, the value that should be submitted when none of the radio buttons is selected. |
1816
|
|
|
* You may set this option to be null to prevent default value submission. |
1817
|
|
|
* If this option is not set, an empty string will be submitted. |
1818
|
|
|
* - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
1819
|
|
|
* This option is ignored if `item` option is set. |
1820
|
|
|
* - separator: string, the HTML code that separates items. |
1821
|
|
|
* - itemOptions: array, the options for generating the radio button tag using [[radio()]]. |
1822
|
|
|
* - item: callable, a callback that can be used to customize the generation of the HTML code |
1823
|
|
|
* corresponding to a single item in $items. The signature of this callback must be: |
1824
|
|
|
* |
1825
|
|
|
* ```php |
1826
|
|
|
* function ($index, $label, $name, $checked, $value) |
1827
|
|
|
* ``` |
1828
|
|
|
* |
1829
|
|
|
* where $index is the zero-based index of the radio button in the whole list; $label |
1830
|
|
|
* is the label for the radio button; and $name, $value and $checked represent the name, |
1831
|
|
|
* value and the checked status of the radio button input. |
1832
|
|
|
* |
1833
|
|
|
* See [[renderTagAttributes()]] for details on how attributes are being rendered. |
1834
|
|
|
* |
1835
|
|
|
* @return string the generated radio button list |
1836
|
|
|
*/ |
1837
|
2 |
|
public static function activeRadioList($model, $attribute, $items, $options = []) |
1838
|
|
|
{ |
1839
|
2 |
|
return static::activeListInput('radioList', $model, $attribute, $items, $options); |
1840
|
|
|
} |
1841
|
|
|
|
1842
|
|
|
/** |
1843
|
|
|
* Generates a list of input fields. |
1844
|
|
|
* This method is mainly called by [[activeListBox()]], [[activeRadioList()]] and [[activeCheckboxList()]]. |
1845
|
|
|
* @param string $type the input type. This can be 'listBox', 'radioList', or 'checkBoxList'. |
1846
|
|
|
* @param Model $model the model object |
1847
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
1848
|
|
|
* about attribute expression. |
1849
|
|
|
* @param array $items the data item used to generate the input fields. |
1850
|
|
|
* The array keys are the input values, and the array values are the corresponding labels. |
1851
|
|
|
* @param array $options options (name => config) for the input list. The supported special options |
1852
|
|
|
* depend on the input type specified by `$type`. |
1853
|
|
|
* @return string the generated input list |
1854
|
|
|
*/ |
1855
|
9 |
|
protected static function activeListInput($type, $model, $attribute, $items, $options = []) |
1856
|
|
|
{ |
1857
|
9 |
|
$name = ArrayHelper::remove($options, 'name', static::getInputName($model, $attribute)); |
1858
|
9 |
|
$selection = ArrayHelper::remove($options, 'value', static::getAttributeValue($model, $attribute)); |
1859
|
9 |
|
if (!array_key_exists('unselect', $options)) { |
1860
|
9 |
|
$options['unselect'] = ''; |
1861
|
|
|
} |
1862
|
9 |
|
if (!array_key_exists('id', $options)) { |
1863
|
7 |
|
$options['id'] = static::getInputId($model, $attribute); |
1864
|
|
|
} |
1865
|
|
|
|
1866
|
9 |
|
return static::$type($name, $selection, $items, $options); |
1867
|
|
|
} |
1868
|
|
|
|
1869
|
|
|
/** |
1870
|
|
|
* Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]]. |
1871
|
|
|
* @param string|array|bool|null $selection the selected value(s). String/boolean for single or array for multiple |
1872
|
|
|
* selection(s). |
1873
|
|
|
* @param array $items the option data items. The array keys are option values, and the array values |
1874
|
|
|
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
1875
|
|
|
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
1876
|
|
|
* If you have a list of data models, you may convert them into the format described above using |
1877
|
|
|
* [[\yii\helpers\ArrayHelper::map()]]. |
1878
|
|
|
* |
1879
|
|
|
* Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
1880
|
|
|
* the labels will also be HTML-encoded. |
1881
|
|
|
* @param array $tagOptions the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call. |
1882
|
|
|
* This method will take out these elements, if any: "prompt", "options" and "groups". See more details |
1883
|
|
|
* in [[dropDownList()]] for the explanation of these elements. |
1884
|
|
|
* |
1885
|
|
|
* @return string the generated list options |
1886
|
|
|
*/ |
1887
|
25 |
|
public static function renderSelectOptions($selection, $items, &$tagOptions = []) |
1888
|
|
|
{ |
1889
|
25 |
|
if (ArrayHelper::isTraversable($selection)) { |
1890
|
4 |
|
$normalizedSelection = []; |
1891
|
4 |
|
foreach (ArrayHelper::toArray($selection) as $selectionItem) { |
1892
|
4 |
|
if (is_bool($selectionItem)) { |
1893
|
1 |
|
$normalizedSelection[] = $selectionItem ? '1' : '0'; |
1894
|
|
|
} else { |
1895
|
4 |
|
$normalizedSelection[] = (string)$selectionItem; |
1896
|
|
|
} |
1897
|
|
|
} |
1898
|
4 |
|
$selection = $normalizedSelection; |
1899
|
24 |
|
} elseif (is_bool($selection)) { |
1900
|
5 |
|
$selection = $selection ? '1' : '0'; |
1901
|
|
|
} |
1902
|
|
|
|
1903
|
25 |
|
$lines = []; |
1904
|
25 |
|
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false); |
1905
|
25 |
|
$encode = ArrayHelper::remove($tagOptions, 'encode', true); |
1906
|
25 |
|
$strict = ArrayHelper::remove($tagOptions, 'strict', false); |
1907
|
25 |
|
if (isset($tagOptions['prompt'])) { |
1908
|
3 |
|
$promptOptions = ['value' => '']; |
1909
|
3 |
|
if (is_string($tagOptions['prompt'])) { |
1910
|
3 |
|
$promptText = $tagOptions['prompt']; |
1911
|
|
|
} else { |
1912
|
1 |
|
$promptText = $tagOptions['prompt']['text']; |
1913
|
1 |
|
$promptOptions = array_merge($promptOptions, $tagOptions['prompt']['options']); |
1914
|
|
|
} |
1915
|
3 |
|
$promptText = $encode ? static::encode($promptText) : $promptText; |
1916
|
3 |
|
if ($encodeSpaces) { |
1917
|
1 |
|
$promptText = str_replace(' ', ' ', $promptText); |
1918
|
|
|
} |
1919
|
3 |
|
$lines[] = static::tag('option', $promptText, $promptOptions); |
1920
|
|
|
} |
1921
|
|
|
|
1922
|
25 |
|
$options = isset($tagOptions['options']) ? $tagOptions['options'] : []; |
1923
|
25 |
|
$groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : []; |
1924
|
25 |
|
unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']); |
1925
|
25 |
|
$options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces); |
1926
|
25 |
|
$options['encode'] = ArrayHelper::getValue($options, 'encode', $encode); |
1927
|
|
|
|
1928
|
25 |
|
foreach ($items as $key => $value) { |
1929
|
24 |
|
if (is_array($value)) { |
1930
|
1 |
|
$groupAttrs = isset($groups[$key]) ? $groups[$key] : []; |
1931
|
1 |
|
if (!isset($groupAttrs['label'])) { |
1932
|
1 |
|
$groupAttrs['label'] = $key; |
1933
|
|
|
} |
1934
|
1 |
|
$attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces, 'encode' => $encode, 'strict' => $strict]; |
1935
|
1 |
|
$content = static::renderSelectOptions($selection, $value, $attrs); |
1936
|
1 |
|
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); |
1937
|
|
|
} else { |
1938
|
24 |
|
$attrs = isset($options[$key]) ? $options[$key] : []; |
1939
|
24 |
|
$attrs['value'] = (string) $key; |
1940
|
24 |
|
if (!array_key_exists('selected', $attrs)) { |
1941
|
24 |
|
$selected = false; |
1942
|
24 |
|
if ($selection !== null) { |
1943
|
20 |
|
if (ArrayHelper::isTraversable($selection)) { |
1944
|
4 |
|
$selected = ArrayHelper::isIn((string)$key, $selection, $strict); |
|
|
|
|
1945
|
19 |
|
} elseif ($key === '' || $selection === '') { |
1946
|
14 |
|
$selected = $selection === $key; |
1947
|
17 |
|
} elseif ($strict) { |
1948
|
7 |
|
$selected = !strcmp((string)$key, (string)$selection); |
1949
|
|
|
} else { |
1950
|
11 |
|
$selected = $selection == $key; |
1951
|
|
|
} |
1952
|
|
|
} |
1953
|
|
|
|
1954
|
24 |
|
$attrs['selected'] = $selected; |
1955
|
|
|
} |
1956
|
24 |
|
$text = $encode ? static::encode($value) : $value; |
1957
|
24 |
|
if ($encodeSpaces) { |
1958
|
2 |
|
$text = str_replace(' ', ' ', $text); |
1959
|
|
|
} |
1960
|
24 |
|
$lines[] = static::tag('option', $text, $attrs); |
1961
|
|
|
} |
1962
|
|
|
} |
1963
|
|
|
|
1964
|
25 |
|
return implode("\n", $lines); |
1965
|
|
|
} |
1966
|
|
|
|
1967
|
|
|
/** |
1968
|
|
|
* Renders the HTML tag attributes. |
1969
|
|
|
* |
1970
|
|
|
* Attributes whose values are of boolean type will be treated as |
1971
|
|
|
* [boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes). |
1972
|
|
|
* |
1973
|
|
|
* Attributes whose values are null will not be rendered. |
1974
|
|
|
* |
1975
|
|
|
* The values of attributes will be HTML-encoded using [[encode()]]. |
1976
|
|
|
* |
1977
|
|
|
* `aria` and `data` attributes get special handling when they are set to an array value. In these cases, |
1978
|
|
|
* the array will be "expanded" and a list of ARIA/data attributes will be rendered. For example, |
1979
|
|
|
* `'aria' => ['role' => 'checkbox', 'value' => 'true']` would be rendered as |
1980
|
|
|
* `aria-role="checkbox" aria-value="true"`. |
1981
|
|
|
* |
1982
|
|
|
* If a nested `data` value is set to an array, it will be JSON-encoded. For example, |
1983
|
|
|
* `'data' => ['params' => ['id' => 1, 'name' => 'yii']]` would be rendered as |
1984
|
|
|
* `data-params='{"id":1,"name":"yii"}'`. |
1985
|
|
|
* |
1986
|
|
|
* @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]]. |
1987
|
|
|
* @return string the rendering result. If the attributes are not empty, they will be rendered |
1988
|
|
|
* into a string with a leading white space (so that it can be directly appended to the tag name |
1989
|
|
|
* in a tag). If there is no attribute, an empty string will be returned. |
1990
|
|
|
* @see addCssClass() |
1991
|
|
|
*/ |
1992
|
280 |
|
public static function renderTagAttributes($attributes) |
1993
|
|
|
{ |
1994
|
280 |
|
if (count($attributes) > 1) { |
1995
|
218 |
|
$sorted = []; |
1996
|
218 |
|
foreach (static::$attributeOrder as $name) { |
1997
|
218 |
|
if (isset($attributes[$name])) { |
1998
|
218 |
|
$sorted[$name] = $attributes[$name]; |
1999
|
|
|
} |
2000
|
|
|
} |
2001
|
218 |
|
$attributes = array_merge($sorted, $attributes); |
2002
|
|
|
} |
2003
|
|
|
|
2004
|
280 |
|
$html = ''; |
2005
|
280 |
|
foreach ($attributes as $name => $value) { |
2006
|
269 |
|
if (is_bool($value)) { |
2007
|
58 |
|
if ($value) { |
2008
|
58 |
|
$html .= " $name"; |
2009
|
|
|
} |
2010
|
269 |
|
} elseif (is_array($value)) { |
2011
|
15 |
|
if (in_array($name, static::$dataAttributes)) { |
2012
|
3 |
|
foreach ($value as $n => $v) { |
2013
|
3 |
|
if (is_array($v)) { |
2014
|
1 |
|
$html .= " $name-$n='" . Json::htmlEncode($v) . "'"; |
2015
|
3 |
|
} elseif (is_bool($v)) { |
2016
|
1 |
|
if ($v) { |
2017
|
1 |
|
$html .= " $name-$n"; |
2018
|
|
|
} |
2019
|
3 |
|
} elseif ($v !== null) { |
2020
|
2 |
|
$html .= " $name-$n=\"" . static::encode($v) . '"'; |
2021
|
|
|
} |
2022
|
|
|
} |
2023
|
14 |
|
} elseif ($name === 'class') { |
2024
|
13 |
|
if (empty($value)) { |
2025
|
12 |
|
continue; |
2026
|
|
|
} |
2027
|
8 |
|
if (static::$normalizeClassAttribute === true && count($value) > 1) { |
2028
|
|
|
// removes duplicate classes |
2029
|
2 |
|
$value = explode(' ', implode(' ', $value)); |
2030
|
2 |
|
$value = array_unique($value); |
2031
|
|
|
} |
2032
|
8 |
|
$html .= " $name=\"" . static::encode(implode(' ', array_filter($value))) . '"'; |
2033
|
2 |
|
} elseif ($name === 'style') { |
2034
|
1 |
|
if (empty($value)) { |
2035
|
1 |
|
continue; |
2036
|
|
|
} |
2037
|
1 |
|
$html .= " $name=\"" . static::encode(static::cssStyleFromArray($value)) . '"'; |
2038
|
|
|
} else { |
2039
|
10 |
|
$html .= " $name='" . Json::htmlEncode($value) . "'"; |
2040
|
|
|
} |
2041
|
259 |
|
} elseif ($value !== null) { |
2042
|
259 |
|
$html .= " $name=\"" . static::encode($value) . '"'; |
2043
|
|
|
} |
2044
|
|
|
} |
2045
|
|
|
|
2046
|
280 |
|
return $html; |
2047
|
|
|
} |
2048
|
|
|
|
2049
|
|
|
/** |
2050
|
|
|
* Adds a CSS class (or several classes) to the specified options. |
2051
|
|
|
* |
2052
|
|
|
* If the CSS class is already in the options, it will not be added again. |
2053
|
|
|
* If class specification at given options is an array, and some class placed there with the named (string) key, |
2054
|
|
|
* overriding of such key will have no effect. For example: |
2055
|
|
|
* |
2056
|
|
|
* ```php |
2057
|
|
|
* $options = ['class' => ['persistent' => 'initial']]; |
2058
|
|
|
* Html::addCssClass($options, ['persistent' => 'override']); |
2059
|
|
|
* var_dump($options['class']); // outputs: array('persistent' => 'initial'); |
2060
|
|
|
* ``` |
2061
|
|
|
* |
2062
|
|
|
* @param array $options the options to be modified. |
2063
|
|
|
* @param string|array $class the CSS class(es) to be added |
2064
|
|
|
* @see removeCssClass() |
2065
|
|
|
*/ |
2066
|
27 |
|
public static function addCssClass(&$options, $class) |
2067
|
|
|
{ |
2068
|
27 |
|
if (isset($options['class'])) { |
2069
|
15 |
|
if (is_array($options['class'])) { |
2070
|
3 |
|
$options['class'] = self::mergeCssClasses($options['class'], (array) $class); |
2071
|
|
|
} else { |
2072
|
13 |
|
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY); |
2073
|
15 |
|
$options['class'] = implode(' ', self::mergeCssClasses($classes, (array) $class)); |
2074
|
|
|
} |
2075
|
|
|
} else { |
2076
|
20 |
|
$options['class'] = $class; |
2077
|
|
|
} |
2078
|
|
|
} |
2079
|
|
|
|
2080
|
|
|
/** |
2081
|
|
|
* Merges already existing CSS classes with new one. |
2082
|
|
|
* This method provides the priority for named existing classes over additional. |
2083
|
|
|
* @param array $existingClasses already existing CSS classes. |
2084
|
|
|
* @param array $additionalClasses CSS classes to be added. |
2085
|
|
|
* @return array merge result. |
2086
|
|
|
* @see addCssClass() |
2087
|
|
|
*/ |
2088
|
15 |
|
private static function mergeCssClasses(array $existingClasses, array $additionalClasses) |
2089
|
|
|
{ |
2090
|
15 |
|
foreach ($additionalClasses as $key => $class) { |
2091
|
15 |
|
if (is_int($key) && !in_array($class, $existingClasses)) { |
2092
|
14 |
|
$existingClasses[] = $class; |
2093
|
2 |
|
} elseif (!isset($existingClasses[$key])) { |
2094
|
1 |
|
$existingClasses[$key] = $class; |
2095
|
|
|
} |
2096
|
|
|
} |
2097
|
|
|
|
2098
|
15 |
|
return static::$normalizeClassAttribute ? array_unique($existingClasses) : $existingClasses; |
2099
|
|
|
} |
2100
|
|
|
|
2101
|
|
|
/** |
2102
|
|
|
* Removes a CSS class from the specified options. |
2103
|
|
|
* @param array $options the options to be modified. |
2104
|
|
|
* @param string|array $class the CSS class(es) to be removed |
2105
|
|
|
* @see addCssClass() |
2106
|
|
|
*/ |
2107
|
1 |
|
public static function removeCssClass(&$options, $class) |
2108
|
|
|
{ |
2109
|
1 |
|
if (isset($options['class'])) { |
2110
|
1 |
|
if (is_array($options['class'])) { |
2111
|
1 |
|
$classes = array_diff($options['class'], (array) $class); |
2112
|
1 |
|
if (empty($classes)) { |
2113
|
1 |
|
unset($options['class']); |
2114
|
|
|
} else { |
2115
|
1 |
|
$options['class'] = $classes; |
2116
|
|
|
} |
2117
|
|
|
} else { |
2118
|
1 |
|
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY); |
2119
|
1 |
|
$classes = array_diff($classes, (array) $class); |
2120
|
1 |
|
if (empty($classes)) { |
2121
|
1 |
|
unset($options['class']); |
2122
|
|
|
} else { |
2123
|
1 |
|
$options['class'] = implode(' ', $classes); |
2124
|
|
|
} |
2125
|
|
|
} |
2126
|
|
|
} |
2127
|
|
|
} |
2128
|
|
|
|
2129
|
|
|
/** |
2130
|
|
|
* Adds the specified CSS style to the HTML options. |
2131
|
|
|
* |
2132
|
|
|
* If the options already contain a `style` element, the new style will be merged |
2133
|
|
|
* with the existing one. If a CSS property exists in both the new and the old styles, |
2134
|
|
|
* the old one may be overwritten if `$overwrite` is true. |
2135
|
|
|
* |
2136
|
|
|
* For example, |
2137
|
|
|
* |
2138
|
|
|
* ```php |
2139
|
|
|
* Html::addCssStyle($options, 'width: 100px; height: 200px'); |
2140
|
|
|
* ``` |
2141
|
|
|
* |
2142
|
|
|
* @param array $options the HTML options to be modified. |
2143
|
|
|
* @param string|array $style the new style string (e.g. `'width: 100px; height: 200px'`) or |
2144
|
|
|
* array (e.g. `['width' => '100px', 'height' => '200px']`). |
2145
|
|
|
* @param bool $overwrite whether to overwrite existing CSS properties if the new style |
2146
|
|
|
* contain them too. |
2147
|
|
|
* @see removeCssStyle() |
2148
|
|
|
* @see cssStyleFromArray() |
2149
|
|
|
* @see cssStyleToArray() |
2150
|
|
|
*/ |
2151
|
1 |
|
public static function addCssStyle(&$options, $style, $overwrite = true) |
2152
|
|
|
{ |
2153
|
1 |
|
if (!empty($options['style'])) { |
2154
|
1 |
|
$oldStyle = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']); |
2155
|
1 |
|
$newStyle = is_array($style) ? $style : static::cssStyleToArray($style); |
2156
|
1 |
|
if (!$overwrite) { |
2157
|
1 |
|
foreach ($newStyle as $property => $value) { |
2158
|
1 |
|
if (isset($oldStyle[$property])) { |
2159
|
1 |
|
unset($newStyle[$property]); |
2160
|
|
|
} |
2161
|
|
|
} |
2162
|
|
|
} |
2163
|
1 |
|
$style = array_merge($oldStyle, $newStyle); |
2164
|
|
|
} |
2165
|
1 |
|
$options['style'] = is_array($style) ? static::cssStyleFromArray($style) : $style; |
2166
|
|
|
} |
2167
|
|
|
|
2168
|
|
|
/** |
2169
|
|
|
* Removes the specified CSS style from the HTML options. |
2170
|
|
|
* |
2171
|
|
|
* For example, |
2172
|
|
|
* |
2173
|
|
|
* ```php |
2174
|
|
|
* Html::removeCssStyle($options, ['width', 'height']); |
2175
|
|
|
* ``` |
2176
|
|
|
* |
2177
|
|
|
* @param array $options the HTML options to be modified. |
2178
|
|
|
* @param string|array $properties the CSS properties to be removed. You may use a string |
2179
|
|
|
* if you are removing a single property. |
2180
|
|
|
* @see addCssStyle() |
2181
|
|
|
*/ |
2182
|
1 |
|
public static function removeCssStyle(&$options, $properties) |
2183
|
|
|
{ |
2184
|
1 |
|
if (!empty($options['style'])) { |
2185
|
1 |
|
$style = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']); |
2186
|
1 |
|
foreach ((array) $properties as $property) { |
2187
|
1 |
|
unset($style[$property]); |
2188
|
|
|
} |
2189
|
1 |
|
$options['style'] = static::cssStyleFromArray($style); |
2190
|
|
|
} |
2191
|
|
|
} |
2192
|
|
|
|
2193
|
|
|
/** |
2194
|
|
|
* Converts a CSS style array into a string representation. |
2195
|
|
|
* |
2196
|
|
|
* For example, |
2197
|
|
|
* |
2198
|
|
|
* ```php |
2199
|
|
|
* print_r(Html::cssStyleFromArray(['width' => '100px', 'height' => '200px'])); |
2200
|
|
|
* // will display: 'width: 100px; height: 200px;' |
2201
|
|
|
* ``` |
2202
|
|
|
* |
2203
|
|
|
* @param array $style the CSS style array. The array keys are the CSS property names, |
2204
|
|
|
* and the array values are the corresponding CSS property values. |
2205
|
|
|
* @return string the CSS style string. If the CSS style is empty, a null will be returned. |
2206
|
|
|
*/ |
2207
|
4 |
|
public static function cssStyleFromArray(array $style) |
2208
|
|
|
{ |
2209
|
4 |
|
$result = ''; |
2210
|
4 |
|
foreach ($style as $name => $value) { |
2211
|
4 |
|
$result .= "$name: $value; "; |
2212
|
|
|
} |
2213
|
|
|
// return null if empty to avoid rendering the "style" attribute |
2214
|
4 |
|
return $result === '' ? null : rtrim($result); |
2215
|
|
|
} |
2216
|
|
|
|
2217
|
|
|
/** |
2218
|
|
|
* Converts a CSS style string into an array representation. |
2219
|
|
|
* |
2220
|
|
|
* The array keys are the CSS property names, and the array values |
2221
|
|
|
* are the corresponding CSS property values. |
2222
|
|
|
* |
2223
|
|
|
* For example, |
2224
|
|
|
* |
2225
|
|
|
* ```php |
2226
|
|
|
* print_r(Html::cssStyleToArray('width: 100px; height: 200px;')); |
2227
|
|
|
* // will display: ['width' => '100px', 'height' => '200px'] |
2228
|
|
|
* ``` |
2229
|
|
|
* |
2230
|
|
|
* @param string $style the CSS style string |
2231
|
|
|
* @return array the array representation of the CSS style |
2232
|
|
|
*/ |
2233
|
3 |
|
public static function cssStyleToArray($style) |
2234
|
|
|
{ |
2235
|
3 |
|
$result = []; |
2236
|
3 |
|
foreach (explode(';', $style) as $property) { |
2237
|
3 |
|
$property = explode(':', $property); |
2238
|
3 |
|
if (count($property) > 1) { |
2239
|
3 |
|
$result[trim($property[0])] = trim($property[1]); |
2240
|
|
|
} |
2241
|
|
|
} |
2242
|
|
|
|
2243
|
3 |
|
return $result; |
2244
|
|
|
} |
2245
|
|
|
|
2246
|
|
|
/** |
2247
|
|
|
* Returns the real attribute name from the given attribute expression. |
2248
|
|
|
* |
2249
|
|
|
* An attribute expression is an attribute name prefixed and/or suffixed with array indexes. |
2250
|
|
|
* It is mainly used in tabular data input and/or input of array type. Below are some examples: |
2251
|
|
|
* |
2252
|
|
|
* - `[0]content` is used in tabular data input to represent the "content" attribute |
2253
|
|
|
* for the first model in tabular input; |
2254
|
|
|
* - `dates[0]` represents the first array element of the "dates" attribute; |
2255
|
|
|
* - `[0]dates[0]` represents the first array element of the "dates" attribute |
2256
|
|
|
* for the first model in tabular input. |
2257
|
|
|
* |
2258
|
|
|
* If `$attribute` has neither prefix nor suffix, it will be returned back without change. |
2259
|
|
|
* @param string $attribute the attribute name or expression |
2260
|
|
|
* @return string the attribute name without prefix and suffix. |
2261
|
|
|
* @throws InvalidArgumentException if the attribute name contains non-word characters. |
2262
|
|
|
*/ |
2263
|
69 |
|
public static function getAttributeName($attribute) |
2264
|
|
|
{ |
2265
|
69 |
|
if (preg_match(static::$attributeRegex, $attribute, $matches)) { |
2266
|
66 |
|
return $matches[2]; |
2267
|
|
|
} |
2268
|
|
|
|
2269
|
3 |
|
throw new InvalidArgumentException('Attribute name must contain word characters only.'); |
2270
|
|
|
} |
2271
|
|
|
|
2272
|
|
|
/** |
2273
|
|
|
* Returns the value of the specified attribute name or expression. |
2274
|
|
|
* |
2275
|
|
|
* For an attribute expression like `[0]dates[0]`, this method will return the value of `$model->dates[0]`. |
2276
|
|
|
* See [[getAttributeName()]] for more details about attribute expression. |
2277
|
|
|
* |
2278
|
|
|
* If an attribute value is an instance of [[ActiveRecordInterface]] or an array of such instances, |
2279
|
|
|
* the primary value(s) of the AR instance(s) will be returned instead. |
2280
|
|
|
* |
2281
|
|
|
* @param Model $model the model object |
2282
|
|
|
* @param string $attribute the attribute name or expression |
2283
|
|
|
* @return string|array the corresponding attribute value |
2284
|
|
|
* @throws InvalidArgumentException if the attribute name contains non-word characters. |
2285
|
|
|
*/ |
2286
|
60 |
|
public static function getAttributeValue($model, $attribute) |
2287
|
|
|
{ |
2288
|
60 |
|
if (!preg_match(static::$attributeRegex, $attribute, $matches)) { |
2289
|
1 |
|
throw new InvalidArgumentException('Attribute name must contain word characters only.'); |
2290
|
|
|
} |
2291
|
59 |
|
$attribute = $matches[2]; |
2292
|
59 |
|
$value = $model->$attribute; |
2293
|
59 |
|
if ($matches[3] !== '') { |
2294
|
|
|
foreach (explode('][', trim($matches[3], '[]')) as $id) { |
2295
|
|
|
if ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$id])) { |
2296
|
|
|
$value = $value[$id]; |
2297
|
|
|
} else { |
2298
|
|
|
return null; |
2299
|
|
|
} |
2300
|
|
|
} |
2301
|
|
|
} |
2302
|
|
|
|
2303
|
|
|
// https://github.com/yiisoft/yii2/issues/1457 |
2304
|
59 |
|
if (is_array($value)) { |
2305
|
1 |
|
foreach ($value as $i => $v) { |
2306
|
1 |
|
if ($v instanceof ActiveRecordInterface) { |
2307
|
1 |
|
$v = $v->getPrimaryKey(false); |
2308
|
1 |
|
$value[$i] = is_array($v) ? json_encode($v) : $v; |
2309
|
|
|
} |
2310
|
|
|
} |
2311
|
59 |
|
} elseif ($value instanceof ActiveRecordInterface) { |
2312
|
1 |
|
$value = $value->getPrimaryKey(false); |
2313
|
|
|
|
2314
|
1 |
|
return is_array($value) ? json_encode($value) : $value; |
2315
|
|
|
} |
2316
|
|
|
|
2317
|
59 |
|
return $value; |
2318
|
|
|
} |
2319
|
|
|
|
2320
|
|
|
/** |
2321
|
|
|
* Generates an appropriate input name for the specified attribute name or expression. |
2322
|
|
|
* |
2323
|
|
|
* This method generates a name that can be used as the input name to collect user input |
2324
|
|
|
* for the specified attribute. The name is generated according to the [[Model::formName|form name]] |
2325
|
|
|
* of the model and the given attribute name. For example, if the form name of the `Post` model |
2326
|
|
|
* is `Post`, then the input name generated for the `content` attribute would be `Post[content]`. |
2327
|
|
|
* |
2328
|
|
|
* See [[getAttributeName()]] for explanation of attribute expression. |
2329
|
|
|
* |
2330
|
|
|
* @param Model $model the model object |
2331
|
|
|
* @param string $attribute the attribute name or expression |
2332
|
|
|
* @return string the generated input name |
2333
|
|
|
* @throws InvalidArgumentException if the attribute name contains non-word characters. |
2334
|
|
|
*/ |
2335
|
93 |
|
public static function getInputName($model, $attribute) |
2336
|
|
|
{ |
2337
|
93 |
|
$formName = $model->formName(); |
2338
|
93 |
|
if (!preg_match(static::$attributeRegex, $attribute, $matches)) { |
2339
|
1 |
|
throw new InvalidArgumentException('Attribute name must contain word characters only.'); |
2340
|
|
|
} |
2341
|
92 |
|
$prefix = $matches[1]; |
2342
|
92 |
|
$attribute = $matches[2]; |
2343
|
92 |
|
$suffix = $matches[3]; |
2344
|
92 |
|
if ($formName === '' && $prefix === '') { |
2345
|
1 |
|
return $attribute . $suffix; |
2346
|
91 |
|
} elseif ($formName !== '') { |
2347
|
90 |
|
return $formName . $prefix . "[$attribute]" . $suffix; |
2348
|
|
|
} |
2349
|
|
|
|
2350
|
1 |
|
throw new InvalidArgumentException(get_class($model) . '::formName() cannot be empty for tabular inputs.'); |
2351
|
|
|
} |
2352
|
|
|
|
2353
|
|
|
/** |
2354
|
|
|
* Converts input name to ID. |
2355
|
|
|
* |
2356
|
|
|
* For example, if `$name` is `Post[content]`, this method will return `post-content`. |
2357
|
|
|
* |
2358
|
|
|
* @param string $name the input name |
2359
|
|
|
* @return string the generated input ID |
2360
|
|
|
* @since 2.0.43 |
2361
|
|
|
*/ |
2362
|
82 |
|
public static function getInputIdByName($name) |
2363
|
|
|
{ |
2364
|
82 |
|
$charset = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
2365
|
82 |
|
$name = mb_strtolower($name, $charset); |
2366
|
82 |
|
return str_replace(['[]', '][', '[', ']', ' ', '.', '--'], ['', '-', '-', '', '-', '-', '-'], $name); |
2367
|
|
|
} |
2368
|
|
|
|
2369
|
|
|
/** |
2370
|
|
|
* Generates an appropriate input ID for the specified attribute name or expression. |
2371
|
|
|
* |
2372
|
|
|
* @param Model $model the model object |
2373
|
|
|
* @param string $attribute the attribute name or expression. See [[getAttributeName()]] for explanation of attribute expression. |
2374
|
|
|
* @return string the generated input ID. |
2375
|
|
|
* @throws InvalidArgumentException if the attribute name contains non-word characters. |
2376
|
|
|
*/ |
2377
|
74 |
|
public static function getInputId($model, $attribute) |
2378
|
|
|
{ |
2379
|
74 |
|
$name = static::getInputName($model, $attribute); |
2380
|
74 |
|
return static::getInputIdByName($name); |
2381
|
|
|
} |
2382
|
|
|
|
2383
|
|
|
/** |
2384
|
|
|
* Escapes regular expression to use in JavaScript. |
2385
|
|
|
* @param string $regexp the regular expression to be escaped. |
2386
|
|
|
* @return string the escaped result. |
2387
|
|
|
* @since 2.0.6 |
2388
|
|
|
*/ |
2389
|
1 |
|
public static function escapeJsRegularExpression($regexp) |
2390
|
|
|
{ |
2391
|
1 |
|
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $regexp); |
2392
|
1 |
|
$deliminator = substr($pattern, 0, 1); |
2393
|
1 |
|
$pos = strrpos($pattern, $deliminator, 1); |
2394
|
1 |
|
$flag = substr($pattern, $pos + 1); |
2395
|
1 |
|
if ($deliminator !== '/') { |
2396
|
1 |
|
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/'; |
2397
|
|
|
} else { |
2398
|
1 |
|
$pattern = substr($pattern, 0, $pos + 1); |
2399
|
|
|
} |
2400
|
1 |
|
if (!empty($flag)) { |
2401
|
1 |
|
$pattern .= preg_replace('/[^igmu]/', '', $flag); |
2402
|
|
|
} |
2403
|
|
|
|
2404
|
1 |
|
return $pattern; |
2405
|
|
|
} |
2406
|
|
|
} |
2407
|
|
|
|