1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Form\FormModelInterface; |
9
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
use Yiisoft\Widget\Widget; |
12
|
|
|
|
13
|
|
|
final class Label extends Widget |
14
|
|
|
{ |
15
|
|
|
private FormModelInterface $data; |
16
|
|
|
private string $attribute; |
17
|
|
|
private array $options = []; |
18
|
|
|
private string $charset = 'UTF-8'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates a label tag for the given form attribute. |
22
|
|
|
* |
23
|
|
|
* @return string the generated label tag. |
24
|
|
|
*/ |
25
|
|
|
public function run(): string |
26
|
|
|
{ |
27
|
|
|
$new = clone $this; |
28
|
|
|
|
29
|
|
|
$for = ArrayHelper::remove( |
30
|
|
|
$new->options, |
31
|
|
|
'for', |
32
|
|
|
HtmlForm::getInputId($new->data, $new->attribute, $new->charset) |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$label = ArrayHelper::remove( |
36
|
|
|
$new->options, |
37
|
|
|
'label', |
38
|
|
|
$new->data->getAttributeLabel(HtmlForm::getAttributeName($new->attribute)) |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
return Html::label($label, $for) |
42
|
|
|
->attributes($new->options) |
43
|
|
|
->render(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set form model, name and options for the widget. |
48
|
|
|
* |
49
|
|
|
* @param FormModelInterface $data Form model. |
50
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
51
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
52
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
57
|
|
|
{ |
58
|
|
|
$new = clone $this; |
59
|
|
|
$new->data = $data; |
60
|
|
|
$new->attribute = $attribute; |
61
|
|
|
$new->options = $options; |
62
|
|
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}. |
67
|
|
|
* |
68
|
|
|
* @param string $value |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
public function charset(string $value): self |
73
|
|
|
{ |
74
|
|
|
$new = clone $this; |
75
|
|
|
$new->charset = $value; |
76
|
|
|
return $new; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The id of a labelable form-related element in the same document as the tag label element. |
81
|
|
|
* |
82
|
|
|
* The first element in the document with an id matching the value of the for attribute is the labeled control for |
83
|
|
|
* this label element, if it is a labelable element. |
84
|
|
|
* |
85
|
|
|
* @param string $value |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
|
|
public function for(string $value): self |
90
|
|
|
{ |
91
|
|
|
$new = clone $this; |
92
|
|
|
$new->options['for'] = $value; |
93
|
|
|
return $new; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This specifies the label to be displayed. |
98
|
|
|
* |
99
|
|
|
* @param string $value |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
* |
103
|
|
|
* Note that this will NOT be encoded. |
104
|
|
|
* - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for |
105
|
|
|
* display (after encoding). |
106
|
|
|
*/ |
107
|
|
|
public function label(string $value): self |
108
|
|
|
{ |
109
|
|
|
$new = clone $this; |
110
|
|
|
$new->options['label'] = $value; |
111
|
|
|
return $new; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|