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