|
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\Html\Tag\CustomTag; |
|
10
|
|
|
use Yiisoft\Widget\Widget; |
|
11
|
|
|
|
|
12
|
|
|
use function call_user_func; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Represent a field validation error (if there are several errors, the first one is used). If field is no validation |
|
16
|
|
|
* error, field part will be hidden. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Error extends Widget |
|
19
|
|
|
{ |
|
20
|
|
|
use FormAttributeTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @psalm-var non-empty-string |
|
24
|
|
|
*/ |
|
25
|
|
|
private string $tag = 'div'; |
|
26
|
|
|
private array $tagAttributes = []; |
|
27
|
|
|
|
|
28
|
|
|
private bool $encode = true; |
|
29
|
|
|
|
|
30
|
|
|
private ?string $message = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var callable|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $messageCallback = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set the container tag name for the error. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $tag Container tag name. |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function tag(string $tag): self |
|
43
|
|
|
{ |
|
44
|
4 |
|
if ($tag === '') { |
|
45
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
$new = clone $this; |
|
49
|
3 |
|
$new->tag = $tag; |
|
50
|
3 |
|
return $new; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function tagAttributes(array $attributes): self |
|
54
|
|
|
{ |
|
55
|
3 |
|
$new = clone $this; |
|
56
|
3 |
|
$new->tagAttributes = $attributes; |
|
57
|
3 |
|
return $new; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Whether content should be HTML-encoded. |
|
62
|
|
|
* |
|
63
|
|
|
* @param bool $value |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function encode(bool $value): self |
|
66
|
|
|
{ |
|
67
|
1 |
|
$new = clone $this; |
|
68
|
1 |
|
$new->encode = $value; |
|
69
|
1 |
|
return $new; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Error message to display. |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function message(?string $value): self |
|
76
|
|
|
{ |
|
77
|
7 |
|
$new = clone $this; |
|
78
|
7 |
|
$new->message = $value; |
|
79
|
7 |
|
return $new; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Callback that will be called to obtain an error message. |
|
84
|
|
|
* |
|
85
|
|
|
* @param callable|null $value |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function messageCallback(?callable $value): self |
|
88
|
|
|
{ |
|
89
|
3 |
|
$new = clone $this; |
|
90
|
3 |
|
$new->messageCallback = $value; |
|
91
|
3 |
|
return $new; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Generates a tag that contains the first validation error of the specified form attribute. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string The generated error tag. |
|
98
|
|
|
*/ |
|
99
|
424 |
|
protected function run(): string |
|
100
|
|
|
{ |
|
101
|
424 |
|
$useModel = $this->hasFormModelAndAttribute(); |
|
102
|
|
|
|
|
103
|
424 |
|
$message = $useModel |
|
104
|
345 |
|
? $this->message ?? $this->getFirstError() |
|
105
|
81 |
|
: $this->message; |
|
106
|
|
|
|
|
107
|
424 |
|
if ($message === null) { |
|
108
|
394 |
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
30 |
|
if ($this->messageCallback !== null) { |
|
112
|
|
|
/** @var string $message */ |
|
113
|
3 |
|
$message = call_user_func( |
|
114
|
3 |
|
$this->messageCallback, |
|
115
|
|
|
$message, |
|
116
|
3 |
|
$useModel ? $this->getFormModel() : null, |
|
117
|
3 |
|
$useModel ? $this->attribute : null |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
30 |
|
return CustomTag::name($this->tag) |
|
122
|
30 |
|
->attributes($this->tagAttributes) |
|
123
|
30 |
|
->content($message) |
|
124
|
30 |
|
->encode($this->encode) |
|
125
|
30 |
|
->render(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|