|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Part; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Form\Field\Base\InputData\InputDataInterface; |
|
9
|
|
|
use Yiisoft\Form\Field\Base\InputData\InputDataTrait; |
|
10
|
|
|
use Yiisoft\Form\ThemeContainer; |
|
11
|
|
|
use Yiisoft\Html\Html; |
|
12
|
|
|
use Yiisoft\Html\Tag\CustomTag; |
|
13
|
|
|
use Yiisoft\Widget\Widget; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represent a field validation error (if there are several errors, the first one is used). If field is no validation |
|
17
|
|
|
* error, field part will be hidden. |
|
18
|
|
|
* |
|
19
|
|
|
* @psalm-type MessageCallback = callable(string, InputDataInterface): string |
|
20
|
|
|
*/ |
|
21
|
|
|
final class Error extends Widget |
|
22
|
|
|
{ |
|
23
|
|
|
use InputDataTrait; |
|
24
|
|
|
|
|
25
|
|
|
private bool $onlyFirst = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @psalm-var non-empty-string |
|
29
|
|
|
*/ |
|
30
|
|
|
private string $tag = 'div'; |
|
31
|
|
|
private array $attributes = []; |
|
32
|
|
|
|
|
33
|
|
|
private bool $encode = true; |
|
34
|
|
|
|
|
35
|
|
|
private string $separator = "\n<br>\n"; |
|
36
|
|
|
|
|
37
|
|
|
private ?string $header = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var non-empty-string|null |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
private ?string $headerTag = 'div'; |
|
43
|
|
|
private array $headerAttributes = []; |
|
44
|
|
|
private bool $headerEncode = true; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @psalm-var non-empty-string |
|
48
|
|
|
*/ |
|
49
|
|
|
private ?string $errorTag = null; |
|
50
|
|
|
private array $errorAttributes = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @psalm-var array<array-key,string>|null |
|
54
|
|
|
*/ |
|
55
|
|
|
private ?array $messages = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var callable|null |
|
59
|
|
|
* @psalm-var MessageCallback|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private $messageCallback = null; |
|
62
|
|
|
|
|
63
|
2 |
|
public function onlyFirst(bool $value = true): self |
|
64
|
|
|
{ |
|
65
|
2 |
|
$new = clone $this; |
|
66
|
2 |
|
$new->onlyFirst = $value; |
|
67
|
2 |
|
return $new; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set the container tag name for the error. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $tag Container tag name. |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function tag(string $tag): self |
|
76
|
|
|
{ |
|
77
|
5 |
|
if ($tag === '') { |
|
78
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
$new = clone $this; |
|
82
|
4 |
|
$new->tag = $tag; |
|
83
|
4 |
|
return $new; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
public function attributes(array $attributes): self |
|
87
|
|
|
{ |
|
88
|
6 |
|
$new = clone $this; |
|
89
|
6 |
|
$new->attributes = $attributes; |
|
90
|
6 |
|
return $new; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
7 |
|
public function addAttributes(array $attributes): self |
|
94
|
|
|
{ |
|
95
|
7 |
|
$new = clone $this; |
|
96
|
7 |
|
$new->attributes = array_merge($this->attributes, $attributes); |
|
97
|
7 |
|
return $new; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set tag ID. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string|null $id Tag ID. |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function id(?string $id): self |
|
106
|
|
|
{ |
|
107
|
3 |
|
$new = clone $this; |
|
108
|
3 |
|
$new->attributes['id'] = $id; |
|
109
|
3 |
|
return $new; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add one or more CSS classes to the tag. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string|null ...$class One or many CSS classes. |
|
116
|
|
|
*/ |
|
117
|
11 |
|
public function addClass(?string ...$class): self |
|
118
|
|
|
{ |
|
119
|
11 |
|
$new = clone $this; |
|
120
|
11 |
|
Html::addCssClass($new->attributes, $class); |
|
121
|
11 |
|
return $new; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Replace tag CSS classes with a new set of classes. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string|null ...$class One or many CSS classes. |
|
128
|
|
|
*/ |
|
129
|
15 |
|
public function class(?string ...$class): self |
|
130
|
|
|
{ |
|
131
|
15 |
|
$new = clone $this; |
|
132
|
15 |
|
$new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null); |
|
133
|
15 |
|
return $new; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Whether error messages should be HTML-encoded. |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function encode(bool $value): self |
|
140
|
|
|
{ |
|
141
|
2 |
|
$new = clone $this; |
|
142
|
2 |
|
$new->encode = $value; |
|
143
|
2 |
|
return $new; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
public function separator(string $separator): self |
|
147
|
|
|
{ |
|
148
|
3 |
|
$new = clone $this; |
|
149
|
3 |
|
$new->separator = $separator; |
|
150
|
3 |
|
return $new; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
8 |
|
public function header(?string $header): self |
|
154
|
|
|
{ |
|
155
|
8 |
|
$new = clone $this; |
|
156
|
8 |
|
$new->header = $header; |
|
157
|
8 |
|
return $new; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set the header tag name. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string|null $tag Header tag name. |
|
164
|
|
|
*/ |
|
165
|
4 |
|
public function headerTag(?string $tag): self |
|
166
|
|
|
{ |
|
167
|
4 |
|
if ($tag === '') { |
|
168
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
3 |
|
$new = clone $this; |
|
172
|
3 |
|
$new->headerTag = $tag; |
|
173
|
3 |
|
return $new; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
3 |
|
public function headerAttributes(array $attributes): self |
|
177
|
|
|
{ |
|
178
|
3 |
|
$new = clone $this; |
|
179
|
3 |
|
$new->headerAttributes = $attributes; |
|
180
|
3 |
|
return $new; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
public function addHeaderAttributes(array $attributes): self |
|
184
|
|
|
{ |
|
185
|
2 |
|
$new = clone $this; |
|
186
|
2 |
|
$new->headerAttributes = array_merge($this->headerAttributes, $attributes); |
|
187
|
2 |
|
return $new; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Whether header content should be HTML-encoded. |
|
192
|
|
|
*/ |
|
193
|
2 |
|
public function headerEncode(bool $encode): self |
|
194
|
|
|
{ |
|
195
|
2 |
|
$new = clone $this; |
|
196
|
2 |
|
$new->headerEncode = $encode; |
|
197
|
2 |
|
return $new; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set an error item tag name. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $tag Error item tag name. |
|
204
|
|
|
*/ |
|
205
|
5 |
|
public function errorTag(string $tag): self |
|
206
|
|
|
{ |
|
207
|
5 |
|
if ($tag === '') { |
|
208
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
4 |
|
$new = clone $this; |
|
212
|
4 |
|
$new->errorTag = $tag; |
|
213
|
4 |
|
return $new; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
3 |
|
public function errorAttributes(array $attributes): self |
|
217
|
|
|
{ |
|
218
|
3 |
|
$new = clone $this; |
|
219
|
3 |
|
$new->errorAttributes = $attributes; |
|
220
|
3 |
|
return $new; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
2 |
|
public function addErrorAttributes(array $attributes): self |
|
224
|
|
|
{ |
|
225
|
2 |
|
$new = clone $this; |
|
226
|
2 |
|
$new->errorAttributes = array_merge($this->errorAttributes, $attributes); |
|
227
|
2 |
|
return $new; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Error messages to display. |
|
232
|
|
|
*/ |
|
233
|
54 |
|
public function message(?string $message, string ...$messages): self |
|
234
|
|
|
{ |
|
235
|
54 |
|
$new = clone $this; |
|
236
|
54 |
|
$new->messages = $message === null ? null : [$message, ...$messages]; |
|
237
|
54 |
|
return $new; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Callback that will be called to obtain an error message. |
|
242
|
|
|
* |
|
243
|
|
|
* @psalm-param MessageCallback|null $callback |
|
244
|
|
|
*/ |
|
245
|
4 |
|
public function messageCallback(?callable $callback): self |
|
246
|
|
|
{ |
|
247
|
4 |
|
$new = clone $this; |
|
248
|
4 |
|
$new->messageCallback = $callback; |
|
249
|
4 |
|
return $new; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Generates a tag that contains the first validation error of the specified form attribute. |
|
254
|
|
|
* |
|
255
|
|
|
* @return string The generated error tag. |
|
256
|
|
|
*/ |
|
257
|
685 |
|
public function render(): string |
|
258
|
|
|
{ |
|
259
|
685 |
|
$messages = $this->messages ?? $this->getInputData()->getValidationErrors(); |
|
260
|
685 |
|
if (empty($messages)) { |
|
261
|
598 |
|
return ''; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
87 |
|
if ($this->onlyFirst) { |
|
265
|
1 |
|
$messages = [reset($messages)]; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
87 |
|
$messageCallback = $this->messageCallback; |
|
269
|
87 |
|
if ($messageCallback !== null) { |
|
270
|
3 |
|
$messages = array_map( |
|
271
|
3 |
|
fn(string $message) => $messageCallback($message, $this->getInputData()), |
|
272
|
3 |
|
$messages, |
|
273
|
3 |
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
87 |
|
$content = []; |
|
277
|
|
|
|
|
278
|
87 |
|
if ($this->header !== null) { |
|
279
|
7 |
|
$content[] = $this->headerTag === null |
|
280
|
1 |
|
? ($this->headerEncode ? Html::encode($this->header) : $this->header) |
|
281
|
6 |
|
: CustomTag::name($this->headerTag) |
|
282
|
6 |
|
->attributes($this->headerAttributes) |
|
283
|
6 |
|
->content($this->header) |
|
284
|
6 |
|
->encode($this->headerEncode) |
|
285
|
6 |
|
->render(); |
|
286
|
7 |
|
$content[] = "\n"; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
87 |
|
$isFirst = true; |
|
290
|
87 |
|
foreach ($messages as $message) { |
|
291
|
87 |
|
if ($isFirst) { |
|
292
|
87 |
|
$isFirst = false; |
|
293
|
|
|
} else { |
|
294
|
12 |
|
$content[] = $this->separator; |
|
295
|
|
|
} |
|
296
|
87 |
|
$content[] = $this->errorTag === null |
|
297
|
84 |
|
? ($this->encode ? Html::encode($message) : $message) |
|
298
|
3 |
|
: CustomTag::name($this->errorTag) |
|
299
|
3 |
|
->attributes($this->errorAttributes) |
|
300
|
3 |
|
->content($message) |
|
301
|
3 |
|
->encode($this->encode) |
|
302
|
3 |
|
->render(); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
87 |
|
return CustomTag::name($this->tag) |
|
306
|
87 |
|
->addAttributes($this->attributes) |
|
307
|
87 |
|
->content(...(count($messages) === 1 ? $content : ["\n", ...$content, "\n"])) |
|
308
|
87 |
|
->encode(false) |
|
309
|
87 |
|
->render(); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
689 |
|
protected static function getThemeConfig(?string $theme): array |
|
313
|
|
|
{ |
|
314
|
689 |
|
return ThemeContainer::getTheme($theme)?->getErrorConfig() ?? []; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|