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