Complex classes like BaseHtml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseHtml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class BaseHtml |
||
26 | { |
||
27 | /** |
||
28 | * @var array list of void elements (element name => 1) |
||
29 | * @see http://www.w3.org/TR/html-markup/syntax.html#void-element |
||
30 | */ |
||
31 | public static $voidElements = [ |
||
32 | 'area' => 1, |
||
33 | 'base' => 1, |
||
34 | 'br' => 1, |
||
35 | 'col' => 1, |
||
36 | 'command' => 1, |
||
37 | 'embed' => 1, |
||
38 | 'hr' => 1, |
||
39 | 'img' => 1, |
||
40 | 'input' => 1, |
||
41 | 'keygen' => 1, |
||
42 | 'link' => 1, |
||
43 | 'meta' => 1, |
||
44 | 'param' => 1, |
||
45 | 'source' => 1, |
||
46 | 'track' => 1, |
||
47 | 'wbr' => 1, |
||
48 | ]; |
||
49 | /** |
||
50 | * @var array the preferred order of attributes in a tag. This mainly affects the order of the attributes |
||
51 | * that are rendered by [[renderTagAttributes()]]. |
||
52 | */ |
||
53 | public static $attributeOrder = [ |
||
54 | 'type', |
||
55 | 'id', |
||
56 | 'class', |
||
57 | 'name', |
||
58 | 'value', |
||
59 | |||
60 | 'href', |
||
61 | 'src', |
||
62 | 'action', |
||
63 | 'method', |
||
64 | |||
65 | 'selected', |
||
66 | 'checked', |
||
67 | 'readonly', |
||
68 | 'disabled', |
||
69 | 'multiple', |
||
70 | |||
71 | 'size', |
||
72 | 'maxlength', |
||
73 | 'width', |
||
74 | 'height', |
||
75 | 'rows', |
||
76 | 'cols', |
||
77 | |||
78 | 'alt', |
||
79 | 'title', |
||
80 | 'rel', |
||
81 | 'media', |
||
82 | ]; |
||
83 | /** |
||
84 | * @var array list of tag attributes that should be specially handled when their values are of array type. |
||
85 | * In particular, if the value of the `data` attribute is `['name' => 'xyz', 'age' => 13]`, two attributes |
||
86 | * will be generated instead of one: `data-name="xyz" data-age="13"`. |
||
87 | * @since 2.0.3 |
||
88 | */ |
||
89 | public static $dataAttributes = ['data', 'data-ng', 'ng']; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Encodes special characters into HTML entities. |
||
94 | * The [[\yii\base\Application::charset|application charset]] will be used for encoding. |
||
95 | * @param string $content the content to be encoded |
||
96 | * @param bool $doubleEncode whether to encode HTML entities in `$content`. If false, |
||
97 | * HTML entities in `$content` will not be further encoded. |
||
98 | * @return string the encoded content |
||
99 | * @see decode() |
||
100 | * @see http://www.php.net/manual/en/function.htmlspecialchars.php |
||
101 | */ |
||
102 | 149 | public static function encode($content, $doubleEncode = true) |
|
106 | |||
107 | /** |
||
108 | * Decodes special HTML entities back to the corresponding characters. |
||
109 | * This is the opposite of [[encode()]]. |
||
110 | * @param string $content the content to be decoded |
||
111 | * @return string the decoded content |
||
112 | * @see encode() |
||
113 | * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php |
||
114 | */ |
||
115 | 1 | public static function decode($content) |
|
119 | |||
120 | /** |
||
121 | * Generates a complete HTML tag. |
||
122 | * @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
||
123 | * @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded. |
||
124 | * If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks. |
||
125 | * @param array $options the HTML tag attributes (HTML options) in terms of name-value pairs. |
||
126 | * These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
127 | * If a value is null, the corresponding attribute will not be rendered. |
||
128 | * |
||
129 | * For example when using `['class' => 'my-class', 'target' => '_blank', 'value' => null]` it will result in the |
||
130 | * html attributes rendered like this: `class="my-class" target="_blank"`. |
||
131 | * |
||
132 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
133 | * |
||
134 | * @return string the generated HTML tag |
||
135 | * @see beginTag() |
||
136 | * @see endTag() |
||
137 | */ |
||
138 | 140 | public static function tag($name, $content = '', $options = []) |
|
146 | |||
147 | /** |
||
148 | * Generates a start tag. |
||
149 | * @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
||
150 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
151 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
152 | * If a value is null, the corresponding attribute will not be rendered. |
||
153 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
154 | * @return string the generated start tag |
||
155 | * @see endTag() |
||
156 | * @see tag() |
||
157 | */ |
||
158 | 29 | public static function beginTag($name, $options = []) |
|
165 | |||
166 | /** |
||
167 | * Generates an end tag. |
||
168 | * @param string|bool|null $name the tag name. If $name is `null` or `false`, the corresponding content will be rendered without any tag. |
||
169 | * @return string the generated end tag |
||
170 | * @see beginTag() |
||
171 | * @see tag() |
||
172 | */ |
||
173 | 8 | public static function endTag($name) |
|
180 | |||
181 | /** |
||
182 | * Generates a style tag. |
||
183 | * @param string $content the style content |
||
184 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
185 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
186 | * If a value is null, the corresponding attribute will not be rendered. |
||
187 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
188 | * @return string the generated style tag |
||
189 | */ |
||
190 | 1 | public static function style($content, $options = []) |
|
194 | |||
195 | /** |
||
196 | * Generates a script tag. |
||
197 | * @param string $content the script content |
||
198 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
199 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
200 | * If a value is null, the corresponding attribute will not be rendered. |
||
201 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
202 | * @return string the generated script tag |
||
203 | */ |
||
204 | 1 | public static function script($content, $options = []) |
|
208 | |||
209 | /** |
||
210 | * Generates a link tag that refers to an external CSS file. |
||
211 | * @param array|string $url the URL of the external CSS file. This parameter will be processed by [[Url::to()]]. |
||
212 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
213 | * |
||
214 | * - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified, |
||
215 | * the generated `link` tag will be enclosed within the conditional comments. This is mainly useful |
||
216 | * for supporting old versions of IE browsers. |
||
217 | * - noscript: if set to true, `link` tag will be wrapped into `<noscript>` tags. |
||
218 | * |
||
219 | * The rest of the options will be rendered as the attributes of the resulting link tag. The values will |
||
220 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
221 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
222 | * @return string the generated link tag |
||
223 | * @see Url::to() |
||
224 | */ |
||
225 | 20 | public static function cssFile($url, $options = []) |
|
243 | |||
244 | /** |
||
245 | * Generates a script tag that refers to an external JavaScript file. |
||
246 | * @param string $url the URL of the external JavaScript file. This parameter will be processed by [[Url::to()]]. |
||
247 | * @param array $options the tag options in terms of name-value pairs. The following option is specially handled: |
||
248 | * |
||
249 | * - condition: specifies the conditional comments for IE, e.g., `lt IE 9`. When this is specified, |
||
250 | * the generated `script` tag will be enclosed within the conditional comments. This is mainly useful |
||
251 | * for supporting old versions of IE browsers. |
||
252 | * |
||
253 | * The rest of the options will be rendered as the attributes of the resulting script tag. The values will |
||
254 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
255 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
256 | * @return string the generated script tag |
||
257 | * @see Url::to() |
||
258 | */ |
||
259 | 22 | public static function jsFile($url, $options = []) |
|
270 | |||
271 | /** |
||
272 | * Wraps given content into conditional comments for IE, e.g., `lt IE 9`. |
||
273 | * @param string $content raw HTML content. |
||
274 | * @param string $condition condition string. |
||
275 | * @return string generated HTML. |
||
276 | */ |
||
277 | 2 | private static function wrapIntoCondition($content, $condition) |
|
284 | |||
285 | /** |
||
286 | * Generates the meta tags containing CSRF token information. |
||
287 | * @return string the generated meta tags |
||
288 | * @see Request::enableCsrfValidation |
||
289 | */ |
||
290 | public static function csrfMetaTags() |
||
300 | |||
301 | /** |
||
302 | * Generates a form start tag. |
||
303 | * @param array|string $action the form action URL. This parameter will be processed by [[Url::to()]]. |
||
304 | * @param string $method the form submission method, such as "post", "get", "put", "delete" (case-insensitive). |
||
305 | * Since most browsers only support "post" and "get", if other methods are given, they will |
||
306 | * be simulated using "post", and a hidden input will be added which contains the actual method type. |
||
307 | * See [[\yii\web\Request::methodParam]] for more details. |
||
308 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
309 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
310 | * If a value is null, the corresponding attribute will not be rendered. |
||
311 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
312 | * |
||
313 | * Special options: |
||
314 | * |
||
315 | * - `csrf`: whether to generate the CSRF hidden input. Defaults to true. |
||
316 | * |
||
317 | * @return string the generated form start tag. |
||
318 | * @see endForm() |
||
319 | */ |
||
320 | 28 | public static function beginForm($action = '', $method = 'post', $options = []) |
|
365 | |||
366 | /** |
||
367 | * Generates a form end tag. |
||
368 | * @return string the generated tag |
||
369 | * @see beginForm() |
||
370 | */ |
||
371 | 27 | public static function endForm() |
|
375 | |||
376 | /** |
||
377 | * Generates a hyperlink tag. |
||
378 | * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
||
379 | * such as an image tag. If this is coming from end users, you should consider [[encode()]] |
||
380 | * it to prevent XSS attacks. |
||
381 | * @param array|string|null $url the URL for the hyperlink tag. This parameter will be processed by [[Url::to()]] |
||
382 | * and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute |
||
383 | * will not be generated. |
||
384 | * |
||
385 | * If you want to use an absolute url you can call [[Url::to()]] yourself, before passing the URL to this method, |
||
386 | * like this: |
||
387 | * |
||
388 | * ```php |
||
389 | * Html::a('link text', Url::to($url, true)) |
||
390 | * ``` |
||
391 | * |
||
392 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
393 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
394 | * If a value is null, the corresponding attribute will not be rendered. |
||
395 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
396 | * @return string the generated hyperlink |
||
397 | * @see \yii\helpers\Url::to() |
||
398 | */ |
||
399 | 13 | public static function a($text, $url = null, $options = []) |
|
406 | |||
407 | /** |
||
408 | * Generates a mailto hyperlink. |
||
409 | * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
||
410 | * such as an image tag. If this is coming from end users, you should consider [[encode()]] |
||
411 | * it to prevent XSS attacks. |
||
412 | * @param string $email email address. If this is null, the first parameter (link body) will be treated |
||
413 | * as the email address and used. |
||
414 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
415 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
416 | * If a value is null, the corresponding attribute will not be rendered. |
||
417 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
418 | * @return string the generated mailto link |
||
419 | */ |
||
420 | 2 | public static function mailto($text, $email = null, $options = []) |
|
421 | { |
||
422 | 2 | $options['href'] = 'mailto:' . ($email === null ? $text : $email); |
|
423 | 2 | return static::tag('a', $text, $options); |
|
424 | } |
||
425 | |||
426 | /** |
||
427 | * Generates an image tag. |
||
428 | * @param array|string $src the image URL. This parameter will be processed by [[Url::to()]]. |
||
429 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
430 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
431 | * If a value is null, the corresponding attribute will not be rendered. |
||
432 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
433 | * @return string the generated image tag |
||
434 | */ |
||
435 | 2 | public static function img($src, $options = []) |
|
443 | |||
444 | /** |
||
445 | * Generates a label tag. |
||
446 | * @param string $content label text. It will NOT be HTML-encoded. Therefore you can pass in HTML code |
||
447 | * such as an image tag. If this is is coming from end users, you should [[encode()]] |
||
448 | * it to prevent XSS attacks. |
||
449 | * @param string $for the ID of the HTML element that this label is associated with. |
||
450 | * If this is null, the "for" attribute will not be generated. |
||
451 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
452 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
453 | * If a value is null, the corresponding attribute will not be rendered. |
||
454 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
455 | * @return string the generated label tag |
||
456 | */ |
||
457 | 13 | public static function label($content, $for = null, $options = []) |
|
462 | |||
463 | /** |
||
464 | * Generates a button tag. |
||
465 | * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
||
466 | * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
||
467 | * you should consider [[encode()]] it to prevent XSS attacks. |
||
468 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
469 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
470 | * If a value is null, the corresponding attribute will not be rendered. |
||
471 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
472 | * @return string the generated button tag |
||
473 | */ |
||
474 | 3 | public static function button($content = 'Button', $options = []) |
|
481 | |||
482 | /** |
||
483 | * Generates a submit button tag. |
||
484 | * |
||
485 | * Be careful when naming form elements such as submit buttons. According to the [jQuery documentation](https://api.jquery.com/submit/) there |
||
486 | * are some reserved names that can cause conflicts, e.g. `submit`, `length`, or `method`. |
||
487 | * |
||
488 | * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
||
489 | * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
||
490 | * you should consider [[encode()]] it to prevent XSS attacks. |
||
491 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
492 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
493 | * If a value is null, the corresponding attribute will not be rendered. |
||
494 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
495 | * @return string the generated submit button tag |
||
496 | */ |
||
497 | 1 | public static function submitButton($content = 'Submit', $options = []) |
|
502 | |||
503 | /** |
||
504 | * Generates a reset button tag. |
||
505 | * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. |
||
506 | * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, |
||
507 | * you should consider [[encode()]] it to prevent XSS attacks. |
||
508 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
509 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
510 | * If a value is null, the corresponding attribute will not be rendered. |
||
511 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
512 | * @return string the generated reset button tag |
||
513 | */ |
||
514 | 1 | public static function resetButton($content = 'Reset', $options = []) |
|
519 | |||
520 | /** |
||
521 | * Generates an input type of the given type. |
||
522 | * @param string $type the type attribute. |
||
523 | * @param string $name the name attribute. If it is null, the name attribute will not be generated. |
||
524 | * @param string $value the value attribute. If it is null, the value attribute will not be generated. |
||
525 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
526 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
527 | * If a value is null, the corresponding attribute will not be rendered. |
||
528 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
529 | * @return string the generated input tag |
||
530 | */ |
||
531 | 47 | public static function input($type, $name = null, $value = null, $options = []) |
|
540 | |||
541 | /** |
||
542 | * Generates an input button. |
||
543 | * @param string $label the value attribute. If it is null, the value attribute will not be generated. |
||
544 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
545 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
546 | * If a value is null, the corresponding attribute will not be rendered. |
||
547 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
548 | * @return string the generated button tag |
||
549 | */ |
||
550 | 1 | public static function buttonInput($label = 'Button', $options = []) |
|
556 | |||
557 | /** |
||
558 | * Generates a submit input button. |
||
559 | * |
||
560 | * Be careful when naming form elements such as submit buttons. According to the [jQuery documentation](https://api.jquery.com/submit/) there |
||
561 | * are some reserved names that can cause conflicts, e.g. `submit`, `length`, or `method`. |
||
562 | * |
||
563 | * @param string $label the value attribute. If it is null, the value attribute will not be generated. |
||
564 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
565 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
566 | * If a value is null, the corresponding attribute will not be rendered. |
||
567 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
568 | * @return string the generated button tag |
||
569 | */ |
||
570 | 1 | public static function submitInput($label = 'Submit', $options = []) |
|
576 | |||
577 | /** |
||
578 | * Generates a reset input button. |
||
579 | * @param string $label the value attribute. If it is null, the value attribute will not be generated. |
||
580 | * @param array $options the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. |
||
581 | * Attributes whose value is null will be ignored and not put in the tag returned. |
||
582 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
583 | * @return string the generated button tag |
||
584 | */ |
||
585 | 1 | public static function resetInput($label = 'Reset', $options = []) |
|
591 | |||
592 | /** |
||
593 | * Generates a text input field. |
||
594 | * @param string $name the name attribute. |
||
595 | * @param string $value the value attribute. If it is null, the value attribute will not be generated. |
||
596 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
597 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
598 | * If a value is null, the corresponding attribute will not be rendered. |
||
599 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
600 | * @return string the generated text input tag |
||
601 | */ |
||
602 | 1 | public static function textInput($name, $value = null, $options = []) |
|
606 | |||
607 | /** |
||
608 | * Generates a hidden input field. |
||
609 | * @param string $name the name attribute. |
||
610 | * @param string $value the value attribute. If it is null, the value attribute will not be generated. |
||
611 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
612 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
613 | * If a value is null, the corresponding attribute will not be rendered. |
||
614 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
615 | * @return string the generated hidden input tag |
||
616 | */ |
||
617 | 32 | public static function hiddenInput($name, $value = null, $options = []) |
|
621 | |||
622 | /** |
||
623 | * Generates a password input field. |
||
624 | * @param string $name the name attribute. |
||
625 | * @param string $value the value attribute. If it is null, the value attribute will not be generated. |
||
626 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
627 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
628 | * If a value is null, the corresponding attribute will not be rendered. |
||
629 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
630 | * @return string the generated password input tag |
||
631 | */ |
||
632 | 1 | public static function passwordInput($name, $value = null, $options = []) |
|
636 | |||
637 | /** |
||
638 | * Generates a file input field. |
||
639 | * To use a file input field, you should set the enclosing form's "enctype" attribute to |
||
640 | * be "multipart/form-data". After the form is submitted, the uploaded file information |
||
641 | * can be obtained via $_FILES[$name] (see PHP documentation). |
||
642 | * @param string $name the name attribute. |
||
643 | * @param string $value the value attribute. If it is null, the value attribute will not be generated. |
||
644 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
645 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
646 | * If a value is null, the corresponding attribute will not be rendered. |
||
647 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
648 | * @return string the generated file input tag |
||
649 | */ |
||
650 | 1 | public static function fileInput($name, $value = null, $options = []) |
|
654 | |||
655 | /** |
||
656 | * Generates a text area input. |
||
657 | * @param string $name the input name |
||
658 | * @param string $value the input value. Note that it will be encoded using [[encode()]]. |
||
659 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
660 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
661 | * If a value is null, the corresponding attribute will not be rendered. |
||
662 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
663 | * @return string the generated text area tag |
||
664 | */ |
||
665 | 5 | public static function textarea($name, $value = '', $options = []) |
|
670 | |||
671 | /** |
||
672 | * Generates a radio button input. |
||
673 | * @param string $name the name attribute. |
||
674 | * @param bool $checked whether the radio button should be checked. |
||
675 | * @param array $options the tag options in terms of name-value pairs. |
||
676 | * See [[booleanInput()]] for details about accepted attributes. |
||
677 | * |
||
678 | * @return string the generated radio button tag |
||
679 | */ |
||
680 | 2 | public static function radio($name, $checked = false, $options = []) |
|
681 | { |
||
682 | 2 | return static::booleanInput('radio', $name, $checked, $options); |
|
683 | } |
||
684 | |||
685 | /** |
||
686 | * Generates a checkbox input. |
||
687 | * @param string $name the name attribute. |
||
688 | * @param bool $checked whether the checkbox should be checked. |
||
689 | * @param array $options the tag options in terms of name-value pairs. |
||
690 | * See [[booleanInput()]] for details about accepted attributes. |
||
691 | * |
||
692 | * @return string the generated checkbox tag |
||
693 | */ |
||
694 | 4 | public static function checkbox($name, $checked = false, $options = []) |
|
695 | { |
||
696 | 4 | return static::booleanInput('checkbox', $name, $checked, $options); |
|
697 | } |
||
698 | |||
699 | /** |
||
700 | * Generates a boolean input. |
||
701 | * @param string $type the input type. This can be either `radio` or `checkbox`. |
||
702 | * @param string $name the name attribute. |
||
703 | * @param bool $checked whether the checkbox should be checked. |
||
704 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
705 | * |
||
706 | * - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute |
||
707 | * is present, a hidden input will be generated so that if the checkbox is not checked and is submitted, |
||
708 | * the value of this attribute will still be submitted to the server via the hidden input. |
||
709 | * - label: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass |
||
710 | * in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. |
||
711 | * When this option is specified, the checkbox will be enclosed by a label tag. |
||
712 | * - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. |
||
713 | * |
||
714 | * The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will |
||
715 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
716 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
717 | * |
||
718 | * @return string the generated checkbox tag |
||
719 | * @since 2.0.9 |
||
720 | */ |
||
721 | 6 | protected static function booleanInput($type, $name, $checked = false, $options = []) |
|
722 | { |
||
723 | 6 | $options['checked'] = (bool) $checked; |
|
724 | 6 | $value = array_key_exists('value', $options) ? $options['value'] : '1'; |
|
725 | 6 | if (isset($options['uncheck'])) { |
|
726 | // add a hidden field so that if the checkbox is not selected, it still submits a value |
||
727 | 2 | $hidden = static::hiddenInput($name, $options['uncheck']); |
|
|
|||
728 | 2 | unset($options['uncheck']); |
|
729 | 2 | } else { |
|
730 | 6 | $hidden = ''; |
|
731 | } |
||
732 | 6 | if (isset($options['label'])) { |
|
733 | 4 | $label = $options['label']; |
|
734 | 4 | $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; |
|
735 | 4 | unset($options['label'], $options['labelOptions']); |
|
736 | 4 | $content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions); |
|
737 | 4 | return $hidden . $content; |
|
738 | } else { |
||
739 | 6 | return $hidden . static::input($type, $name, $value, $options); |
|
740 | } |
||
741 | } |
||
742 | |||
743 | /** |
||
744 | * Generates a drop-down list. |
||
745 | * @param string $name the input name |
||
746 | * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
||
747 | * @param array $items the option data items. The array keys are option values, and the array values |
||
748 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
749 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
750 | * If you have a list of data models, you may convert them into the format described above using |
||
751 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
752 | * |
||
753 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
754 | * the labels will also be HTML-encoded. |
||
755 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
756 | * |
||
757 | * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
||
758 | * to override the value and to set other tag attributes: |
||
759 | * |
||
760 | * ```php |
||
761 | * ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
||
762 | * ``` |
||
763 | * |
||
764 | * - options: array, the attributes for the select option tags. The array keys must be valid option values, |
||
765 | * and the array values are the extra attributes for the corresponding option tags. For example, |
||
766 | * |
||
767 | * ```php |
||
768 | * [ |
||
769 | * 'value1' => ['disabled' => true], |
||
770 | * 'value2' => ['label' => 'value 2'], |
||
771 | * ]; |
||
772 | * ``` |
||
773 | * |
||
774 | * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
||
775 | * except that the array keys represent the optgroup labels specified in $items. |
||
776 | * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
||
777 | * Defaults to false. |
||
778 | * - encode: bool, whether to encode option prompt and option value characters. |
||
779 | * Defaults to `true`. This option is available since 2.0.3. |
||
780 | * |
||
781 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
782 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
783 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
784 | * |
||
785 | * @return string the generated drop-down list tag |
||
786 | */ |
||
787 | 2 | public static function dropDownList($name, $selection = null, $items = [], $options = []) |
|
788 | { |
||
789 | 2 | if (!empty($options['multiple'])) { |
|
790 | return static::listBox($name, $selection, $items, $options); |
||
791 | } |
||
792 | 2 | $options['name'] = $name; |
|
793 | 2 | unset($options['unselect']); |
|
794 | 2 | $selectOptions = static::renderSelectOptions($selection, $items, $options); |
|
795 | 2 | return static::tag('select', "\n" . $selectOptions . "\n", $options); |
|
796 | } |
||
797 | |||
798 | /** |
||
799 | * Generates a list box. |
||
800 | * @param string $name the input name |
||
801 | * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
||
802 | * @param array $items the option data items. The array keys are option values, and the array values |
||
803 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
804 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
805 | * If you have a list of data models, you may convert them into the format described above using |
||
806 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
807 | * |
||
808 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
809 | * the labels will also be HTML-encoded. |
||
810 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
811 | * |
||
812 | * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
||
813 | * to override the value and to set other tag attributes: |
||
814 | * |
||
815 | * ```php |
||
816 | * ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
||
817 | * ``` |
||
818 | * |
||
819 | * - options: array, the attributes for the select option tags. The array keys must be valid option values, |
||
820 | * and the array values are the extra attributes for the corresponding option tags. For example, |
||
821 | * |
||
822 | * ```php |
||
823 | * [ |
||
824 | * 'value1' => ['disabled' => true], |
||
825 | * 'value2' => ['label' => 'value 2'], |
||
826 | * ]; |
||
827 | * ``` |
||
828 | * |
||
829 | * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
||
830 | * except that the array keys represent the optgroup labels specified in $items. |
||
831 | * - unselect: string, the value that will be submitted when no option is selected. |
||
832 | * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple |
||
833 | * mode, we can still obtain the posted unselect value. |
||
834 | * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
||
835 | * Defaults to false. |
||
836 | * - encode: bool, whether to encode option prompt and option value characters. |
||
837 | * Defaults to `true`. This option is available since 2.0.3. |
||
838 | * |
||
839 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
840 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
841 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
842 | * |
||
843 | * @return string the generated list box tag |
||
844 | */ |
||
845 | 3 | public static function listBox($name, $selection = null, $items = [], $options = []) |
|
867 | |||
868 | /** |
||
869 | * Generates a list of checkboxes. |
||
870 | * A checkbox list allows multiple selection, like [[listBox()]]. |
||
871 | * As a result, the corresponding submitted value is an array. |
||
872 | * @param string $name the name attribute of each checkbox. |
||
873 | * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
||
874 | * @param array $items the data item used to generate the checkboxes. |
||
875 | * The array keys are the checkbox values, while the array values are the corresponding labels. |
||
876 | * @param array $options options (name => config) for the checkbox list container tag. |
||
877 | * The following options are specially handled: |
||
878 | * |
||
879 | * - tag: string|false, the tag name of the container element. False to render checkbox without container. |
||
880 | * See also [[tag()]]. |
||
881 | * - unselect: string, the value that should be submitted when none of the checkboxes is selected. |
||
882 | * By setting this option, a hidden input will be generated. |
||
883 | * - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
||
884 | * This option is ignored if `item` option is set. |
||
885 | * - separator: string, the HTML code that separates items. |
||
886 | * - itemOptions: array, the options for generating the checkbox tag using [[checkbox()]]. |
||
887 | * - item: callable, a callback that can be used to customize the generation of the HTML code |
||
888 | * corresponding to a single item in $items. The signature of this callback must be: |
||
889 | * |
||
890 | * ```php |
||
891 | * function ($index, $label, $name, $checked, $value) |
||
892 | * ``` |
||
893 | * |
||
894 | * where $index is the zero-based index of the checkbox in the whole list; $label |
||
895 | * is the label for the checkbox; and $name, $value and $checked represent the name, |
||
896 | * value and the checked status of the checkbox input, respectively. |
||
897 | * |
||
898 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
899 | * |
||
900 | * @return string the generated checkbox list |
||
901 | */ |
||
902 | 1 | public static function checkboxList($name, $selection = null, $items = [], $options = []) |
|
948 | |||
949 | /** |
||
950 | * Generates a list of radio buttons. |
||
951 | * A radio button list is like a checkbox list, except that it only allows single selection. |
||
952 | * @param string $name the name attribute of each radio button. |
||
953 | * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
||
954 | * @param array $items the data item used to generate the radio buttons. |
||
955 | * The array keys are the radio button values, while the array values are the corresponding labels. |
||
956 | * @param array $options options (name => config) for the radio button list container tag. |
||
957 | * The following options are specially handled: |
||
958 | * |
||
959 | * - tag: string|false, the tag name of the container element. False to render radio buttons without container. |
||
960 | * See also [[tag()]]. |
||
961 | * - unselect: string, the value that should be submitted when none of the radio buttons is selected. |
||
962 | * By setting this option, a hidden input will be generated. |
||
963 | * - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
||
964 | * This option is ignored if `item` option is set. |
||
965 | * - separator: string, the HTML code that separates items. |
||
966 | * - itemOptions: array, the options for generating the radio button tag using [[radio()]]. |
||
967 | * - item: callable, a callback that can be used to customize the generation of the HTML code |
||
968 | * corresponding to a single item in $items. The signature of this callback must be: |
||
969 | * |
||
970 | * ```php |
||
971 | * function ($index, $label, $name, $checked, $value) |
||
972 | * ``` |
||
973 | * |
||
974 | * where $index is the zero-based index of the radio button in the whole list; $label |
||
975 | * is the label for the radio button; and $name, $value and $checked represent the name, |
||
976 | * value and the checked status of the radio button input, respectively. |
||
977 | * |
||
978 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
979 | * |
||
980 | * @return string the generated radio button list |
||
981 | */ |
||
982 | 1 | public static function radioList($name, $selection = null, $items = [], $options = []) |
|
1017 | |||
1018 | /** |
||
1019 | * Generates an unordered list. |
||
1020 | * @param array|\Traversable $items the items for generating the list. Each item generates a single list item. |
||
1021 | * Note that items will be automatically HTML encoded if `$options['encode']` is not set or true. |
||
1022 | * @param array $options options (name => config) for the radio button list. The following options are supported: |
||
1023 | * |
||
1024 | * - encode: boolean, whether to HTML-encode the items. Defaults to true. |
||
1025 | * This option is ignored if the `item` option is specified. |
||
1026 | * - separator: string, the HTML code that separates items. Defaults to a simple newline (`"\n"`). |
||
1027 | * This option is available since version 2.0.7. |
||
1028 | * - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified. |
||
1029 | * - item: callable, a callback that is used to generate each individual list item. |
||
1030 | * The signature of this callback must be: |
||
1031 | * |
||
1032 | * ```php |
||
1033 | * function ($item, $index) |
||
1034 | * ``` |
||
1035 | * |
||
1036 | * where $index is the array key corresponding to `$item` in `$items`. The callback should return |
||
1037 | * the whole list item tag. |
||
1038 | * |
||
1039 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1040 | * |
||
1041 | * @return string the generated unordered list. An empty list tag will be returned if `$items` is empty. |
||
1042 | */ |
||
1043 | 4 | public static function ul($items, $options = []) |
|
1070 | |||
1071 | /** |
||
1072 | * Generates an ordered list. |
||
1073 | * @param array|\Traversable $items the items for generating the list. Each item generates a single list item. |
||
1074 | * Note that items will be automatically HTML encoded if `$options['encode']` is not set or true. |
||
1075 | * @param array $options options (name => config) for the radio button list. The following options are supported: |
||
1076 | * |
||
1077 | * - encode: boolean, whether to HTML-encode the items. Defaults to true. |
||
1078 | * This option is ignored if the `item` option is specified. |
||
1079 | * - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified. |
||
1080 | * - item: callable, a callback that is used to generate each individual list item. |
||
1081 | * The signature of this callback must be: |
||
1082 | * |
||
1083 | * ```php |
||
1084 | * function ($item, $index) |
||
1085 | * ``` |
||
1086 | * |
||
1087 | * where $index is the array key corresponding to `$item` in `$items`. The callback should return |
||
1088 | * the whole list item tag. |
||
1089 | * |
||
1090 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1091 | * |
||
1092 | * @return string the generated ordered list. An empty string is returned if `$items` is empty. |
||
1093 | */ |
||
1094 | 1 | public static function ol($items, $options = []) |
|
1099 | |||
1100 | /** |
||
1101 | * Generates a label tag for the given model attribute. |
||
1102 | * The label text is the label associated with the attribute, obtained via [[Model::getAttributeLabel()]]. |
||
1103 | * @param Model $model the model object |
||
1104 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1105 | * about attribute expression. |
||
1106 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1107 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1108 | * If a value is null, the corresponding attribute will not be rendered. |
||
1109 | * The following options are specially handled: |
||
1110 | * |
||
1111 | * - label: this specifies the label to be displayed. Note that this will NOT be [[encode()|encoded]]. |
||
1112 | * If this is not set, [[Model::getAttributeLabel()]] will be called to get the label for display |
||
1113 | * (after encoding). |
||
1114 | * |
||
1115 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1116 | * |
||
1117 | * @return string the generated label tag |
||
1118 | */ |
||
1119 | 8 | public static function activeLabel($model, $attribute, $options = []) |
|
1126 | |||
1127 | /** |
||
1128 | * Generates a hint tag for the given model attribute. |
||
1129 | * The hint text is the hint associated with the attribute, obtained via [[Model::getAttributeHint()]]. |
||
1130 | * If no hint content can be obtained, method will return an empty string. |
||
1131 | * @param Model $model the model object |
||
1132 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1133 | * about attribute expression. |
||
1134 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1135 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1136 | * If a value is null, the corresponding attribute will not be rendered. |
||
1137 | * The following options are specially handled: |
||
1138 | * |
||
1139 | * - hint: this specifies the hint to be displayed. Note that this will NOT be [[encode()|encoded]]. |
||
1140 | * If this is not set, [[Model::getAttributeHint()]] will be called to get the hint for display |
||
1141 | * (without encoding). |
||
1142 | * |
||
1143 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1144 | * |
||
1145 | * @return string the generated hint tag |
||
1146 | * @since 2.0.4 |
||
1147 | */ |
||
1148 | 8 | public static function activeHint($model, $attribute, $options = []) |
|
1159 | |||
1160 | /** |
||
1161 | * Generates a summary of the validation errors. |
||
1162 | * If there is no validation error, an empty error summary markup will still be generated, but it will be hidden. |
||
1163 | * @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
||
1164 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
1165 | * |
||
1166 | * - header: string, the header HTML for the error summary. If not set, a default prompt string will be used. |
||
1167 | * - footer: string, the footer HTML for the error summary. Defaults to empty string. |
||
1168 | * - encode: boolean, if set to false then the error messages won't be encoded. Defaults to `true`. |
||
1169 | * - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise |
||
1170 | * only the first error message for each attribute will be shown. Defaults to `false`. |
||
1171 | * Option is available since 2.0.10. |
||
1172 | * |
||
1173 | * The rest of the options will be rendered as the attributes of the container tag. |
||
1174 | * |
||
1175 | * @return string the generated error summary |
||
1176 | */ |
||
1177 | 7 | public static function errorSummary($models, $options = []) |
|
1213 | |||
1214 | /** |
||
1215 | * Generates a tag that contains the first validation error of the specified model attribute. |
||
1216 | * Note that even if there is no validation error, this method will still return an empty error tag. |
||
1217 | * @param Model $model the model object |
||
1218 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1219 | * about attribute expression. |
||
1220 | * @param array $options the tag options in terms of name-value pairs. The values will be HTML-encoded |
||
1221 | * using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
1222 | * |
||
1223 | * The following options are specially handled: |
||
1224 | * |
||
1225 | * - tag: this specifies the tag name. If not set, "div" will be used. |
||
1226 | * See also [[tag()]]. |
||
1227 | * - encode: boolean, if set to false then the error message won't be encoded. |
||
1228 | * |
||
1229 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1230 | * |
||
1231 | * @return string the generated label tag |
||
1232 | */ |
||
1233 | 6 | public static function error($model, $attribute, $options = []) |
|
1241 | |||
1242 | /** |
||
1243 | * Generates an input tag for the given model attribute. |
||
1244 | * This method will generate the "name" and "value" tag attributes automatically for the model attribute |
||
1245 | * unless they are explicitly specified in `$options`. |
||
1246 | * @param string $type the input type (e.g. 'text', 'password') |
||
1247 | * @param Model $model the model object |
||
1248 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1249 | * about attribute expression. |
||
1250 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1251 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1252 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1253 | * @return string the generated input tag |
||
1254 | */ |
||
1255 | 16 | public static function activeInput($type, $model, $attribute, $options = []) |
|
1264 | |||
1265 | /** |
||
1266 | * If `maxlength` option is set true and the model attribute is validated by a string validator, |
||
1267 | * the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. |
||
1268 | * @param Model $model the model object |
||
1269 | * @param string $attribute the attribute name or expression. |
||
1270 | * @param array $options the tag options in terms of name-value pairs. |
||
1271 | */ |
||
1272 | 16 | private static function normalizeMaxLength($model, $attribute, &$options) |
|
1285 | |||
1286 | /** |
||
1287 | * Generates a text input tag for the given model attribute. |
||
1288 | * This method will generate the "name" and "value" tag attributes automatically for the model attribute |
||
1289 | * unless they are explicitly specified in `$options`. |
||
1290 | * @param Model $model the model object |
||
1291 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1292 | * about attribute expression. |
||
1293 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1294 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1295 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1296 | * The following special options are recognized: |
||
1297 | * |
||
1298 | * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
||
1299 | * by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. |
||
1300 | * This is available since version 2.0.3. |
||
1301 | * |
||
1302 | * @return string the generated input tag |
||
1303 | */ |
||
1304 | 9 | public static function activeTextInput($model, $attribute, $options = []) |
|
1309 | |||
1310 | /** |
||
1311 | * Generates a hidden input tag for the given model attribute. |
||
1312 | * This method will generate the "name" and "value" tag attributes automatically for the model attribute |
||
1313 | * unless they are explicitly specified in `$options`. |
||
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. These will be rendered as |
||
1318 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1319 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1320 | * @return string the generated input tag |
||
1321 | */ |
||
1322 | 2 | public static function activeHiddenInput($model, $attribute, $options = []) |
|
1326 | |||
1327 | /** |
||
1328 | * Generates a password input tag for the given model attribute. |
||
1329 | * This method will generate the "name" and "value" tag attributes automatically for the model attribute |
||
1330 | * unless they are explicitly specified in `$options`. |
||
1331 | * @param Model $model the model object |
||
1332 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1333 | * about attribute expression. |
||
1334 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1335 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1336 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1337 | * The following special options are recognized: |
||
1338 | * |
||
1339 | * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
||
1340 | * by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. |
||
1341 | * This option is available since version 2.0.6. |
||
1342 | * |
||
1343 | * @return string the generated input tag |
||
1344 | */ |
||
1345 | 3 | public static function activePasswordInput($model, $attribute, $options = []) |
|
1350 | |||
1351 | /** |
||
1352 | * Generates a file input tag for the given model attribute. |
||
1353 | * This method will generate the "name" and "value" tag attributes automatically for the model attribute |
||
1354 | * unless they are explicitly specified in `$options`. |
||
1355 | * @param Model $model the model object |
||
1356 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1357 | * about attribute expression. |
||
1358 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1359 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1360 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1361 | * @return string the generated input tag |
||
1362 | */ |
||
1363 | 1 | public static function activeFileInput($model, $attribute, $options = []) |
|
1374 | |||
1375 | /** |
||
1376 | * Generates a textarea tag for the given model attribute. |
||
1377 | * The model attribute value will be used as the content in the textarea. |
||
1378 | * @param Model $model the model object |
||
1379 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1380 | * about attribute expression. |
||
1381 | * @param array $options the tag options in terms of name-value pairs. These will be rendered as |
||
1382 | * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. |
||
1383 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1384 | * The following special options are recognized: |
||
1385 | * |
||
1386 | * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated |
||
1387 | * by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]]. |
||
1388 | * This option is available since version 2.0.6. |
||
1389 | * |
||
1390 | * @return string the generated textarea tag |
||
1391 | */ |
||
1392 | 4 | public static function activeTextarea($model, $attribute, $options = []) |
|
1407 | |||
1408 | /** |
||
1409 | * Generates a radio button tag together with a label for the given model attribute. |
||
1410 | * This method will generate the "checked" tag attribute according to the model attribute value. |
||
1411 | * @param Model $model the model object |
||
1412 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1413 | * about attribute expression. |
||
1414 | * @param array $options the tag options in terms of name-value pairs. |
||
1415 | * See [[booleanInput()]] for details about accepted attributes. |
||
1416 | * |
||
1417 | * @return string the generated radio button tag |
||
1418 | */ |
||
1419 | public static function activeRadio($model, $attribute, $options = []) |
||
1423 | |||
1424 | /** |
||
1425 | * Generates a checkbox tag together with a label for the given model attribute. |
||
1426 | * This method will generate the "checked" tag attribute according to the model attribute value. |
||
1427 | * @param Model $model the model object |
||
1428 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1429 | * about attribute expression. |
||
1430 | * @param array $options the tag options in terms of name-value pairs. |
||
1431 | * See [[booleanInput()]] for details about accepted attributes. |
||
1432 | * |
||
1433 | * @return string the generated checkbox tag |
||
1434 | */ |
||
1435 | public static function activeCheckbox($model, $attribute, $options = []) |
||
1439 | |||
1440 | /** |
||
1441 | * Generates a boolean input |
||
1442 | * This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]]. |
||
1443 | * @param string $type the input type. This can be either `radio` or `checkbox`. |
||
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. |
||
1448 | * See [[booleanInput()]] for details about accepted attributes. |
||
1449 | * @return string the generated input element |
||
1450 | * @since 2.0.9 |
||
1451 | */ |
||
1452 | protected static function activeBooleanInput($type, $model, $attribute, $options = []) |
||
1475 | |||
1476 | /** |
||
1477 | * Generates a drop-down list for the given model attribute. |
||
1478 | * The selection of the drop-down list is taken from the value of the model attribute. |
||
1479 | * @param Model $model the model object |
||
1480 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1481 | * about attribute expression. |
||
1482 | * @param array $items the option data items. The array keys are option values, and the array values |
||
1483 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
1484 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
1485 | * If you have a list of data models, you may convert them into the format described above using |
||
1486 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
1487 | * |
||
1488 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
1489 | * the labels will also be HTML-encoded. |
||
1490 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
1491 | * |
||
1492 | * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
||
1493 | * to override the value and to set other tag attributes: |
||
1494 | * |
||
1495 | * ```php |
||
1496 | * ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
||
1497 | * ``` |
||
1498 | * |
||
1499 | * - options: array, the attributes for the select option tags. The array keys must be valid option values, |
||
1500 | * and the array values are the extra attributes for the corresponding option tags. For example, |
||
1501 | * |
||
1502 | * ```php |
||
1503 | * [ |
||
1504 | * 'value1' => ['disabled' => true], |
||
1505 | * 'value2' => ['label' => 'value 2'], |
||
1506 | * ]; |
||
1507 | * ``` |
||
1508 | * |
||
1509 | * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
||
1510 | * except that the array keys represent the optgroup labels specified in $items. |
||
1511 | * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
||
1512 | * Defaults to false. |
||
1513 | * - encode: bool, whether to encode option prompt and option value characters. |
||
1514 | * Defaults to `true`. This option is available since 2.0.3. |
||
1515 | * |
||
1516 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
1517 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
1518 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1519 | * |
||
1520 | * @return string the generated drop-down list tag |
||
1521 | */ |
||
1522 | 1 | public static function activeDropDownList($model, $attribute, $items, $options = []) |
|
1530 | |||
1531 | /** |
||
1532 | * Generates a list box. |
||
1533 | * The selection of the list box is taken from the value of the model attribute. |
||
1534 | * @param Model $model the model object |
||
1535 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1536 | * about attribute expression. |
||
1537 | * @param array $items the option data items. The array keys are option values, and the array values |
||
1538 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
1539 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
1540 | * If you have a list of data models, you may convert them into the format described above using |
||
1541 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
1542 | * |
||
1543 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
1544 | * the labels will also be HTML-encoded. |
||
1545 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
1546 | * |
||
1547 | * - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array |
||
1548 | * to override the value and to set other tag attributes: |
||
1549 | * |
||
1550 | * ```php |
||
1551 | * ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], |
||
1552 | * ``` |
||
1553 | * |
||
1554 | * - options: array, the attributes for the select option tags. The array keys must be valid option values, |
||
1555 | * and the array values are the extra attributes for the corresponding option tags. For example, |
||
1556 | * |
||
1557 | * ```php |
||
1558 | * [ |
||
1559 | * 'value1' => ['disabled' => true], |
||
1560 | * 'value2' => ['label' => 'value 2'], |
||
1561 | * ]; |
||
1562 | * ``` |
||
1563 | * |
||
1564 | * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', |
||
1565 | * except that the array keys represent the optgroup labels specified in $items. |
||
1566 | * - unselect: string, the value that will be submitted when no option is selected. |
||
1567 | * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple |
||
1568 | * mode, we can still obtain the posted unselect value. |
||
1569 | * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. |
||
1570 | * Defaults to false. |
||
1571 | * - encode: bool, whether to encode option prompt and option value characters. |
||
1572 | * Defaults to `true`. This option is available since 2.0.3. |
||
1573 | * |
||
1574 | * The rest of the options will be rendered as the attributes of the resulting tag. The values will |
||
1575 | * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. |
||
1576 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1577 | * |
||
1578 | * @return string the generated list box tag |
||
1579 | */ |
||
1580 | 2 | public static function activeListBox($model, $attribute, $items, $options = []) |
|
1584 | |||
1585 | /** |
||
1586 | * Generates a list of checkboxes. |
||
1587 | * A checkbox list allows multiple selection, like [[listBox()]]. |
||
1588 | * As a result, the corresponding submitted value is an array. |
||
1589 | * The selection of the checkbox list is taken from the value of the model attribute. |
||
1590 | * @param Model $model the model object |
||
1591 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1592 | * about attribute expression. |
||
1593 | * @param array $items the data item used to generate the checkboxes. |
||
1594 | * The array keys are the checkbox values, and the array values are the corresponding labels. |
||
1595 | * Note that the labels will NOT be HTML-encoded, while the values will. |
||
1596 | * @param array $options options (name => config) for the checkbox list container tag. |
||
1597 | * The following options are specially handled: |
||
1598 | * |
||
1599 | * - tag: string|false, the tag name of the container element. False to render checkbox without container. |
||
1600 | * See also [[tag()]]. |
||
1601 | * - unselect: string, the value that should be submitted when none of the checkboxes is selected. |
||
1602 | * You may set this option to be null to prevent default value submission. |
||
1603 | * If this option is not set, an empty string will be submitted. |
||
1604 | * - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
||
1605 | * This option is ignored if `item` option is set. |
||
1606 | * - separator: string, the HTML code that separates items. |
||
1607 | * - itemOptions: array, the options for generating the checkbox tag using [[checkbox()]]. |
||
1608 | * - item: callable, a callback that can be used to customize the generation of the HTML code |
||
1609 | * corresponding to a single item in $items. The signature of this callback must be: |
||
1610 | * |
||
1611 | * ```php |
||
1612 | * function ($index, $label, $name, $checked, $value) |
||
1613 | * ``` |
||
1614 | * |
||
1615 | * where $index is the zero-based index of the checkbox in the whole list; $label |
||
1616 | * is the label for the checkbox; and $name, $value and $checked represent the name, |
||
1617 | * value and the checked status of the checkbox input. |
||
1618 | * |
||
1619 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1620 | * |
||
1621 | * @return string the generated checkbox list |
||
1622 | */ |
||
1623 | public static function activeCheckboxList($model, $attribute, $items, $options = []) |
||
1627 | |||
1628 | /** |
||
1629 | * Generates a list of radio buttons. |
||
1630 | * A radio button list is like a checkbox list, except that it only allows single selection. |
||
1631 | * The selection of the radio buttons 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 data item used to generate the radio buttons. |
||
1636 | * The array keys are the radio values, and the array values are the corresponding labels. |
||
1637 | * Note that the labels will NOT be HTML-encoded, while the values will. |
||
1638 | * @param array $options options (name => config) for the radio button list container tag. |
||
1639 | * The following options are specially handled: |
||
1640 | * |
||
1641 | * - tag: string|false, the tag name of the container element. False to render radio button without container. |
||
1642 | * See also [[tag()]]. |
||
1643 | * - unselect: string, the value that should be submitted when none of the radio buttons is selected. |
||
1644 | * You may set this option to be null to prevent default value submission. |
||
1645 | * If this option is not set, an empty string will be submitted. |
||
1646 | * - encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. |
||
1647 | * This option is ignored if `item` option is set. |
||
1648 | * - separator: string, the HTML code that separates items. |
||
1649 | * - itemOptions: array, the options for generating the radio button tag using [[radio()]]. |
||
1650 | * - item: callable, a callback that can be used to customize the generation of the HTML code |
||
1651 | * corresponding to a single item in $items. The signature of this callback must be: |
||
1652 | * |
||
1653 | * ```php |
||
1654 | * function ($index, $label, $name, $checked, $value) |
||
1655 | * ``` |
||
1656 | * |
||
1657 | * where $index is the zero-based index of the radio button in the whole list; $label |
||
1658 | * is the label for the radio button; and $name, $value and $checked represent the name, |
||
1659 | * value and the checked status of the radio button input. |
||
1660 | * |
||
1661 | * See [[renderTagAttributes()]] for details on how attributes are being rendered. |
||
1662 | * |
||
1663 | * @return string the generated radio button list |
||
1664 | */ |
||
1665 | public static function activeRadioList($model, $attribute, $items, $options = []) |
||
1669 | |||
1670 | /** |
||
1671 | * Generates a list of input fields. |
||
1672 | * This method is mainly called by [[activeListBox()]], [[activeRadioList()]] and [[activeCheckboxList()]]. |
||
1673 | * @param string $type the input type. This can be 'listBox', 'radioList', or 'checkBoxList'. |
||
1674 | * @param Model $model the model object |
||
1675 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format |
||
1676 | * about attribute expression. |
||
1677 | * @param array $items the data item used to generate the input fields. |
||
1678 | * The array keys are the input values, and the array values are the corresponding labels. |
||
1679 | * Note that the labels will NOT be HTML-encoded, while the values will. |
||
1680 | * @param array $options options (name => config) for the input list. The supported special options |
||
1681 | * depend on the input type specified by `$type`. |
||
1682 | * @return string the generated input list |
||
1683 | */ |
||
1684 | 3 | protected static function activeListInput($type, $model, $attribute, $items, $options = []) |
|
1696 | |||
1697 | /** |
||
1698 | * Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]]. |
||
1699 | * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s). |
||
1700 | * @param array $items the option data items. The array keys are option values, and the array values |
||
1701 | * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). |
||
1702 | * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. |
||
1703 | * If you have a list of data models, you may convert them into the format described above using |
||
1704 | * [[\yii\helpers\ArrayHelper::map()]]. |
||
1705 | * |
||
1706 | * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in |
||
1707 | * the labels will also be HTML-encoded. |
||
1708 | * @param array $tagOptions the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call. |
||
1709 | * This method will take out these elements, if any: "prompt", "options" and "groups". See more details |
||
1710 | * in [[dropDownList()]] for the explanation of these elements. |
||
1711 | * |
||
1712 | * @return string the generated list options |
||
1713 | */ |
||
1714 | 6 | public static function renderSelectOptions($selection, $items, &$tagOptions = []) |
|
1767 | |||
1768 | /** |
||
1769 | * Renders the HTML tag attributes. |
||
1770 | * |
||
1771 | * Attributes whose values are of boolean type will be treated as |
||
1772 | * [boolean attributes](http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes). |
||
1773 | * |
||
1774 | * Attributes whose values are null will not be rendered. |
||
1775 | * |
||
1776 | * The values of attributes will be HTML-encoded using [[encode()]]. |
||
1777 | * |
||
1778 | * The "data" attribute is specially handled when it is receiving an array value. In this case, |
||
1779 | * the array will be "expanded" and a list data attributes will be rendered. For example, |
||
1780 | * if `'data' => ['id' => 1, 'name' => 'yii']`, then this will be rendered: |
||
1781 | * `data-id="1" data-name="yii"`. |
||
1782 | * Additionally `'data' => ['params' => ['id' => 1, 'name' => 'yii'], 'status' => 'ok']` will be rendered as: |
||
1783 | * `data-params='{"id":1,"name":"yii"}' data-status="ok"`. |
||
1784 | * |
||
1785 | * @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]]. |
||
1786 | * @return string the rendering result. If the attributes are not empty, they will be rendered |
||
1787 | * into a string with a leading white space (so that it can be directly appended to the tag name |
||
1788 | * in a tag. If there is no attribute, an empty string will be returned. |
||
1789 | */ |
||
1790 | 144 | public static function renderTagAttributes($attributes) |
|
1837 | |||
1838 | /** |
||
1839 | * Adds a CSS class (or several classes) to the specified options. |
||
1840 | * If the CSS class is already in the options, it will not be added again. |
||
1841 | * If class specification at given options is an array, and some class placed there with the named (string) key, |
||
1842 | * overriding of such key will have no effect. For example: |
||
1843 | * |
||
1844 | * ```php |
||
1845 | * $options = ['class' => ['persistent' => 'initial']]; |
||
1846 | * Html::addCssClass($options, ['persistent' => 'override']); |
||
1847 | * var_dump($options['class']); // outputs: array('persistent' => 'initial'); |
||
1848 | * ``` |
||
1849 | * |
||
1850 | * @param array $options the options to be modified. |
||
1851 | * @param string|array $class the CSS class(es) to be added |
||
1852 | */ |
||
1853 | 5 | public static function addCssClass(&$options, $class) |
|
1866 | |||
1867 | /** |
||
1868 | * Merges already existing CSS classes with new one. |
||
1869 | * This method provides the priority for named existing classes over additional. |
||
1870 | * @param array $existingClasses already existing CSS classes. |
||
1871 | * @param array $additionalClasses CSS classes to be added. |
||
1872 | * @return array merge result. |
||
1873 | */ |
||
1874 | 4 | private static function mergeCssClasses(array $existingClasses, array $additionalClasses) |
|
1885 | |||
1886 | /** |
||
1887 | * Removes a CSS class from the specified options. |
||
1888 | * @param array $options the options to be modified. |
||
1889 | * @param string|array $class the CSS class(es) to be removed |
||
1890 | */ |
||
1891 | 1 | public static function removeCssClass(&$options, $class) |
|
1912 | |||
1913 | /** |
||
1914 | * Adds the specified CSS style to the HTML options. |
||
1915 | * |
||
1916 | * If the options already contain a `style` element, the new style will be merged |
||
1917 | * with the existing one. If a CSS property exists in both the new and the old styles, |
||
1918 | * the old one may be overwritten if `$overwrite` is true. |
||
1919 | * |
||
1920 | * For example, |
||
1921 | * |
||
1922 | * ```php |
||
1923 | * Html::addCssStyle($options, 'width: 100px; height: 200px'); |
||
1924 | * ``` |
||
1925 | * |
||
1926 | * @param array $options the HTML options to be modified. |
||
1927 | * @param string|array $style the new style string (e.g. `'width: 100px; height: 200px'`) or |
||
1928 | * array (e.g. `['width' => '100px', 'height' => '200px']`). |
||
1929 | * @param bool $overwrite whether to overwrite existing CSS properties if the new style |
||
1930 | * contain them too. |
||
1931 | * @see removeCssStyle() |
||
1932 | * @see cssStyleFromArray() |
||
1933 | * @see cssStyleToArray() |
||
1934 | */ |
||
1935 | 1 | public static function addCssStyle(&$options, $style, $overwrite = true) |
|
1951 | |||
1952 | /** |
||
1953 | * Removes the specified CSS style from the HTML options. |
||
1954 | * |
||
1955 | * For example, |
||
1956 | * |
||
1957 | * ```php |
||
1958 | * Html::removeCssStyle($options, ['width', 'height']); |
||
1959 | * ``` |
||
1960 | * |
||
1961 | * @param array $options the HTML options to be modified. |
||
1962 | * @param string|array $properties the CSS properties to be removed. You may use a string |
||
1963 | * if you are removing a single property. |
||
1964 | * @see addCssStyle() |
||
1965 | */ |
||
1966 | 1 | public static function removeCssStyle(&$options, $properties) |
|
1976 | |||
1977 | /** |
||
1978 | * Converts a CSS style array into a string representation. |
||
1979 | * |
||
1980 | * For example, |
||
1981 | * |
||
1982 | * ```php |
||
1983 | * print_r(Html::cssStyleFromArray(['width' => '100px', 'height' => '200px'])); |
||
1984 | * // will display: 'width: 100px; height: 200px;' |
||
1985 | * ``` |
||
1986 | * |
||
1987 | * @param array $style the CSS style array. The array keys are the CSS property names, |
||
1988 | * and the array values are the corresponding CSS property values. |
||
1989 | * @return string the CSS style string. If the CSS style is empty, a null will be returned. |
||
1990 | */ |
||
1991 | 4 | public static function cssStyleFromArray(array $style) |
|
2000 | |||
2001 | /** |
||
2002 | * Converts a CSS style string into an array representation. |
||
2003 | * |
||
2004 | * The array keys are the CSS property names, and the array values |
||
2005 | * are the corresponding CSS property values. |
||
2006 | * |
||
2007 | * For example, |
||
2008 | * |
||
2009 | * ```php |
||
2010 | * print_r(Html::cssStyleToArray('width: 100px; height: 200px;')); |
||
2011 | * // will display: ['width' => '100px', 'height' => '200px'] |
||
2012 | * ``` |
||
2013 | * |
||
2014 | * @param string $style the CSS style string |
||
2015 | * @return array the array representation of the CSS style |
||
2016 | */ |
||
2017 | 3 | public static function cssStyleToArray($style) |
|
2028 | |||
2029 | /** |
||
2030 | * Returns the real attribute name from the given attribute expression. |
||
2031 | * |
||
2032 | * An attribute expression is an attribute name prefixed and/or suffixed with array indexes. |
||
2033 | * It is mainly used in tabular data input and/or input of array type. Below are some examples: |
||
2034 | * |
||
2035 | * - `[0]content` is used in tabular data input to represent the "content" attribute |
||
2036 | * for the first model in tabular input; |
||
2037 | * - `dates[0]` represents the first array element of the "dates" attribute; |
||
2038 | * - `[0]dates[0]` represents the first array element of the "dates" attribute |
||
2039 | * for the first model in tabular input. |
||
2040 | * |
||
2041 | * If `$attribute` has neither prefix nor suffix, it will be returned back without change. |
||
2042 | * @param string $attribute the attribute name or expression |
||
2043 | * @return string the attribute name without prefix and suffix. |
||
2044 | * @throws InvalidParamException if the attribute name contains non-word characters. |
||
2045 | */ |
||
2046 | 22 | public static function getAttributeName($attribute) |
|
2054 | |||
2055 | /** |
||
2056 | * Returns the value of the specified attribute name or expression. |
||
2057 | * |
||
2058 | * For an attribute expression like `[0]dates[0]`, this method will return the value of `$model->dates[0]`. |
||
2059 | * See [[getAttributeName()]] for more details about attribute expression. |
||
2060 | * |
||
2061 | * If an attribute value is an instance of [[ActiveRecordInterface]] or an array of such instances, |
||
2062 | * the primary value(s) of the AR instance(s) will be returned instead. |
||
2063 | * |
||
2064 | * @param Model $model the model object |
||
2065 | * @param string $attribute the attribute name or expression |
||
2066 | * @return string|array the corresponding attribute value |
||
2067 | * @throws InvalidParamException if the attribute name contains non-word characters. |
||
2068 | */ |
||
2069 | 22 | public static function getAttributeValue($model, $attribute) |
|
2102 | |||
2103 | /** |
||
2104 | * Generates an appropriate input name for the specified attribute name or expression. |
||
2105 | * |
||
2106 | * This method generates a name that can be used as the input name to collect user input |
||
2107 | * for the specified attribute. The name is generated according to the [[Model::formName|form name]] |
||
2108 | * of the model and the given attribute name. For example, if the form name of the `Post` model |
||
2109 | * is `Post`, then the input name generated for the `content` attribute would be `Post[content]`. |
||
2110 | * |
||
2111 | * See [[getAttributeName()]] for explanation of attribute expression. |
||
2112 | * |
||
2113 | * @param Model $model the model object |
||
2114 | * @param string $attribute the attribute name or expression |
||
2115 | * @return string the generated input name |
||
2116 | * @throws InvalidParamException if the attribute name contains non-word characters. |
||
2117 | */ |
||
2118 | 33 | public static function getInputName($model, $attribute) |
|
2135 | |||
2136 | /** |
||
2137 | * Generates an appropriate input ID for the specified attribute name or expression. |
||
2138 | * |
||
2139 | * This method converts the result [[getInputName()]] into a valid input ID. |
||
2140 | * For example, if [[getInputName()]] returns `Post[content]`, this method will return `post-content`. |
||
2141 | * @param Model $model the model object |
||
2142 | * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for explanation of attribute expression. |
||
2143 | * @return string the generated input ID |
||
2144 | * @throws InvalidParamException if the attribute name contains non-word characters. |
||
2145 | */ |
||
2146 | 30 | public static function getInputId($model, $attribute) |
|
2151 | |||
2152 | /** |
||
2153 | * Escapes regular expression to use in JavaScript |
||
2154 | * @param string $regexp the regular expression to be escaped. |
||
2155 | * @return string the escaped result. |
||
2156 | * @since 2.0.6 |
||
2157 | */ |
||
2158 | public static function escapeJsRegularExpression($regexp) |
||
2175 | } |
||
2176 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: