|
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
|
|
|
final class Error extends Widget |
|
15
|
|
|
{ |
|
16
|
|
|
use FormAttributeTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @psalm-var non-empty-string |
|
20
|
|
|
*/ |
|
21
|
|
|
private string $tag = 'div'; |
|
22
|
|
|
private array $attributes = []; |
|
23
|
|
|
|
|
24
|
|
|
private bool $encode = true; |
|
25
|
|
|
|
|
26
|
|
|
private ?string $message = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var callable|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $messageCallback = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the container tag name for the hint. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $tag Container tag name. Set to empty value to render error messages without container tag. |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function tag(string $tag): self |
|
41
|
|
|
{ |
|
42
|
5 |
|
if ($tag === '') { |
|
43
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
$new = clone $this; |
|
47
|
4 |
|
$new->tag = $tag; |
|
48
|
4 |
|
return $new; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
public function tagAttributes(array $attributes): self |
|
52
|
|
|
{ |
|
53
|
3 |
|
$new = clone $this; |
|
54
|
3 |
|
$new->attributes = $attributes; |
|
55
|
3 |
|
return $new; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Whether content should be HTML-encoded. |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool $value |
|
62
|
|
|
* |
|
63
|
|
|
* @return static |
|
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
|
|
|
* @return static |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function message(?string $value): self |
|
78
|
|
|
{ |
|
79
|
6 |
|
$new = clone $this; |
|
80
|
6 |
|
$new->message = $value; |
|
81
|
6 |
|
return $new; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Callback that will be called to obtain an error message. |
|
86
|
|
|
* |
|
87
|
|
|
* The signature of the callback must be: |
|
88
|
|
|
* |
|
89
|
|
|
* ```php |
|
90
|
|
|
* [$FormModel, function()] |
|
91
|
|
|
* ``` |
|
92
|
|
|
* |
|
93
|
|
|
* @param callable|null $value |
|
94
|
|
|
* |
|
95
|
|
|
* @return static |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function messageCallback(?callable $value): self |
|
98
|
|
|
{ |
|
99
|
3 |
|
$new = clone $this; |
|
100
|
3 |
|
$new->messageCallback = $value; |
|
101
|
3 |
|
return $new; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Generates a tag that contains the first validation error of the specified form attribute. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string The generated error tag. |
|
108
|
|
|
*/ |
|
109
|
44 |
|
protected function run(): string |
|
110
|
|
|
{ |
|
111
|
44 |
|
$message = $this->getFirstError(); |
|
112
|
44 |
|
if ($message === null) { |
|
113
|
19 |
|
return ''; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
25 |
|
if ($this->message !== null) { |
|
117
|
4 |
|
$message = $this->message; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
25 |
|
if ($this->messageCallback !== null) { |
|
121
|
|
|
/** @var string $message */ |
|
122
|
2 |
|
$message = call_user_func($this->messageCallback, $this->getFormModel(), $this->attribute, $message); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
25 |
|
return CustomTag::name($this->tag) |
|
126
|
25 |
|
->attributes($this->attributes) |
|
127
|
25 |
|
->content($message) |
|
128
|
25 |
|
->encode($this->encode) |
|
129
|
25 |
|
->render(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|