|
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\FormAttributeTrait; |
|
9
|
|
|
use Yiisoft\Form\ThemeContainer; |
|
10
|
|
|
use Yiisoft\Html\Html; |
|
11
|
|
|
use Yiisoft\Html\Tag\CustomTag; |
|
12
|
|
|
use Yiisoft\Widget\Widget; |
|
13
|
|
|
|
|
14
|
|
|
use function call_user_func; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Represent a field validation error (if there are several errors, the first one is used). If field is no validation |
|
18
|
|
|
* error, field part will be hidden. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Error extends Widget |
|
21
|
|
|
{ |
|
22
|
|
|
use FormAttributeTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-var non-empty-string |
|
26
|
|
|
*/ |
|
27
|
|
|
private string $tag = 'div'; |
|
28
|
|
|
private array $attributes = []; |
|
29
|
|
|
|
|
30
|
|
|
private bool $encode = true; |
|
31
|
|
|
|
|
32
|
|
|
private ?string $message = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var callable|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $messageCallback = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set the container tag name for the error. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $tag Container tag name. |
|
43
|
4 |
|
*/ |
|
44
|
|
|
public function tag(string $tag): self |
|
45
|
4 |
|
{ |
|
46
|
1 |
|
if ($tag === '') { |
|
47
|
|
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
48
|
|
|
} |
|
49
|
3 |
|
|
|
50
|
3 |
|
$new = clone $this; |
|
51
|
3 |
|
$new->tag = $tag; |
|
52
|
|
|
return $new; |
|
53
|
|
|
} |
|
54
|
4 |
|
|
|
55
|
|
|
public function attributes(array $attributes): self |
|
56
|
4 |
|
{ |
|
57
|
4 |
|
$new = clone $this; |
|
58
|
4 |
|
$new->attributes = $attributes; |
|
59
|
|
|
return $new; |
|
60
|
|
|
} |
|
61
|
5 |
|
|
|
62
|
|
|
public function addAttributes(array $attributes): self |
|
63
|
5 |
|
{ |
|
64
|
5 |
|
$new = clone $this; |
|
65
|
5 |
|
$new->attributes = array_merge($this->attributes, $attributes); |
|
66
|
|
|
return $new; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set tag ID. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string|null $id Tag ID. |
|
73
|
2 |
|
*/ |
|
74
|
|
|
public function id(?string $id): self |
|
75
|
2 |
|
{ |
|
76
|
2 |
|
$new = clone $this; |
|
77
|
2 |
|
$new->attributes['id'] = $id; |
|
78
|
|
|
return $new; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Add one or more CSS classes to the tag. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string|null ...$class One or many CSS classes. |
|
85
|
10 |
|
*/ |
|
86
|
|
|
public function addClass(?string ...$class): self |
|
87
|
10 |
|
{ |
|
88
|
10 |
|
$new = clone $this; |
|
89
|
10 |
|
Html::addCssClass( |
|
90
|
10 |
|
$new->attributes, |
|
91
|
10 |
|
array_filter($class, static fn ($c) => $c !== null), |
|
92
|
10 |
|
); |
|
93
|
|
|
return $new; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Replace tag CSS classes with a new set of classes. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string|null ...$class One or many CSS classes. |
|
100
|
9 |
|
*/ |
|
101
|
|
|
public function class(?string ...$class): self |
|
102
|
9 |
|
{ |
|
103
|
9 |
|
$new = clone $this; |
|
104
|
9 |
|
$new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null); |
|
105
|
|
|
return $new; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Whether content should be HTML-encoded. |
|
110
|
1 |
|
*/ |
|
111
|
|
|
public function encode(bool $value): self |
|
112
|
1 |
|
{ |
|
113
|
1 |
|
$new = clone $this; |
|
114
|
1 |
|
$new->encode = $value; |
|
115
|
|
|
return $new; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Error message to display. |
|
120
|
12 |
|
*/ |
|
121
|
|
|
public function message(?string $value): self |
|
122
|
12 |
|
{ |
|
123
|
12 |
|
$new = clone $this; |
|
124
|
12 |
|
$new->message = $value; |
|
125
|
|
|
return $new; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Callback that will be called to obtain an error message. |
|
130
|
3 |
|
*/ |
|
131
|
|
|
public function messageCallback(?callable $value): self |
|
132
|
3 |
|
{ |
|
133
|
3 |
|
$new = clone $this; |
|
134
|
3 |
|
$new->messageCallback = $value; |
|
135
|
|
|
return $new; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Generates a tag that contains the first validation error of the specified form attribute. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string The generated error tag. |
|
142
|
519 |
|
*/ |
|
143
|
|
|
public function render(): string |
|
144
|
519 |
|
{ |
|
145
|
|
|
$useModel = $this->hasFormModelAndAttribute(); |
|
146
|
519 |
|
|
|
147
|
408 |
|
$message = $useModel |
|
148
|
113 |
|
? $this->message ?? $this->getFirstError() |
|
149
|
|
|
: $this->message; |
|
150
|
519 |
|
|
|
151
|
462 |
|
if ($message === null) { |
|
152
|
|
|
return ''; |
|
153
|
|
|
} |
|
154
|
57 |
|
|
|
155
|
|
|
if ($this->messageCallback !== null) { |
|
156
|
3 |
|
/** @var string $message */ |
|
157
|
3 |
|
$message = call_user_func( |
|
158
|
3 |
|
$this->messageCallback, |
|
159
|
3 |
|
$message, |
|
160
|
3 |
|
$useModel ? $this->getFormModel() : null, |
|
161
|
3 |
|
$useModel ? $this->formAttribute : null |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
57 |
|
|
|
165
|
57 |
|
return CustomTag::name($this->tag) |
|
166
|
57 |
|
->addAttributes($this->attributes) |
|
167
|
57 |
|
->content($message) |
|
168
|
57 |
|
->encode($this->encode) |
|
169
|
|
|
->render(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
protected static function getThemeConfig(?string $theme): array |
|
173
|
|
|
{ |
|
174
|
|
|
return ThemeContainer::getTheme($theme)?->getErrorConfig() ?? []; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|