1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\Attribute; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
|
12
|
|
|
trait GlobalAttributes |
13
|
|
|
{ |
14
|
|
|
private string $autoIdPrefix = ''; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Focus on the control (put cursor into it) when the page loads. |
18
|
|
|
* Only one form element could be in focus at the same time. |
19
|
|
|
* |
20
|
|
|
* @param bool $value |
21
|
|
|
* |
22
|
|
|
* @return static |
23
|
|
|
* |
24
|
|
|
* @link https://www.w3.org/TR/html52/sec-forms.html#autofocusing-a-form-control-the-autofocus-attribute |
25
|
|
|
*/ |
26
|
2 |
|
public function autofocus(bool $value = true): self |
27
|
|
|
{ |
28
|
2 |
|
$new = clone $this; |
29
|
2 |
|
$new->attributes['autofocus'] = $value; |
|
|
|
|
30
|
2 |
|
return $new; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The prefix to the automatically generated widget IDs. |
35
|
|
|
* |
36
|
|
|
* @param string $value |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
3 |
|
public function autoIdPrefix(string $value): self |
41
|
|
|
{ |
42
|
3 |
|
$new = clone $this; |
43
|
3 |
|
$new->autoIdPrefix = $value; |
44
|
3 |
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set whether the element is disabled or not. |
49
|
|
|
* |
50
|
|
|
* If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out |
51
|
|
|
* text. |
52
|
|
|
* If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event |
53
|
|
|
* will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as |
54
|
|
|
* this will suggest you can set it to `false` to enable the element again, which is not the case. |
55
|
|
|
* |
56
|
|
|
* @param bool $value |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
* |
60
|
|
|
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled |
61
|
|
|
*/ |
62
|
1 |
|
public function disabled(bool $value = true): self |
63
|
|
|
{ |
64
|
1 |
|
$new = clone $this; |
65
|
1 |
|
$new->attributes['disabled'] = $value; |
|
|
|
|
66
|
1 |
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the id |
71
|
|
|
* attribute of a {@see Form} element in the same document. |
72
|
|
|
* |
73
|
|
|
* @param string $value |
74
|
|
|
* |
75
|
|
|
* @return static |
76
|
|
|
* |
77
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
78
|
|
|
*/ |
79
|
2 |
|
public function form(string $value): self |
80
|
|
|
{ |
81
|
2 |
|
$new = clone $this; |
82
|
2 |
|
$new->attributes['form'] = $value; |
|
|
|
|
83
|
2 |
|
return $new; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the ID of the widget. |
88
|
|
|
* |
89
|
|
|
* @param string|null $id |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
* |
93
|
|
|
* @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute |
94
|
|
|
*/ |
95
|
3 |
|
public function id(?string $id): self |
96
|
|
|
{ |
97
|
3 |
|
$new = clone $this; |
98
|
3 |
|
$new->attributes['id'] = $id; |
|
|
|
|
99
|
3 |
|
return $new; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The name part of the name/value pair associated with this element for the purposes of form submission. |
104
|
|
|
* |
105
|
|
|
* @param string|null The name of the widget. |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @return static |
108
|
|
|
* |
109
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name |
110
|
|
|
*/ |
111
|
2 |
|
public function name(?string $value): self |
112
|
|
|
{ |
113
|
2 |
|
$new = clone $this; |
114
|
2 |
|
$new->attributes['name'] = $value; |
|
|
|
|
115
|
2 |
|
return $new; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If it is required to fill in a value in order to submit the form. |
120
|
|
|
* |
121
|
|
|
* @param bool $value |
122
|
|
|
* |
123
|
|
|
* @return static |
124
|
|
|
* |
125
|
|
|
* @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute |
126
|
|
|
*/ |
127
|
1 |
|
public function required(bool $value = true): self |
128
|
|
|
{ |
129
|
1 |
|
$new = clone $this; |
130
|
1 |
|
$new->attributes['required'] = $value; |
|
|
|
|
131
|
1 |
|
return $new; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* The tabindex global attribute indicates that its element can be focused, and where it participates in sequential |
136
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
137
|
|
|
* |
138
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
139
|
|
|
* |
140
|
|
|
* - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard |
141
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
142
|
|
|
* with JavaScript. |
143
|
|
|
* - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is |
144
|
|
|
* defined by the document's source order. |
145
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
146
|
|
|
* defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3". |
147
|
|
|
* |
148
|
|
|
* @param int $value |
149
|
|
|
* |
150
|
|
|
* @return static |
151
|
|
|
* |
152
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
153
|
|
|
*/ |
154
|
2 |
|
public function tabIndex(int $value = 0): self |
155
|
|
|
{ |
156
|
2 |
|
$new = clone $this; |
157
|
2 |
|
$new->attributes['tabindex'] = $value; |
|
|
|
|
158
|
2 |
|
return $new; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* The title global attribute contains text representing advisory information related to the element it belongs to. |
163
|
|
|
* |
164
|
|
|
* @param string $value |
165
|
|
|
* |
166
|
|
|
* @return static |
167
|
|
|
* |
168
|
|
|
* @link https://html.spec.whatwg.org/multipage/dom.html#attr-title |
169
|
|
|
*/ |
170
|
6 |
|
public function title(string $value): self |
171
|
|
|
{ |
172
|
6 |
|
$new = clone $this; |
173
|
6 |
|
$new->attributes['title'] = $value; |
|
|
|
|
174
|
6 |
|
return $new; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* The value of the radio button. |
179
|
|
|
* |
180
|
|
|
* @param scalar|Stringable|null $value |
181
|
|
|
* |
182
|
|
|
* @return static |
183
|
|
|
* |
184
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-value |
185
|
|
|
*/ |
186
|
24 |
|
public function value($value): self |
187
|
|
|
{ |
188
|
24 |
|
$new = clone $this; |
189
|
24 |
|
$new->attributes['value'] = $value; |
|
|
|
|
190
|
24 |
|
return $new; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Generates a unique ID for the attribute without model, if it does not have one yet. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
34 |
|
protected function getIdWithoutModel(): ?string |
199
|
|
|
{ |
200
|
34 |
|
$id = ArrayHelper::remove($this->attributes, 'id', ''); |
201
|
|
|
|
202
|
34 |
|
if (!is_string($id) && null !== $id) { |
203
|
1 |
|
throw new InvalidArgumentException('Attribute "id" must be a string or null.'); |
204
|
|
|
} |
205
|
|
|
|
206
|
33 |
|
if ($id === '') { |
207
|
30 |
|
$id = Html::generateId($this->autoIdPrefix); |
208
|
|
|
} |
209
|
|
|
|
210
|
33 |
|
return $id === null ? null : $id; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|