1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
use Yiisoft\Form\Helper\HtmlFormErrors; |
9
|
|
|
use Yiisoft\Html\Tag\CustomTag; |
10
|
|
|
use Yiisoft\Widget\Widget; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The Error widget displays an error message. |
14
|
|
|
* |
15
|
|
|
* @psalm-suppress MissingConstructor |
16
|
|
|
*/ |
17
|
|
|
final class Error extends Widget |
18
|
|
|
{ |
19
|
|
|
private string $attribute = ''; |
20
|
|
|
private bool $encode = true; |
21
|
|
|
private FormModelInterface $formModel; |
22
|
|
|
private string $message = ''; |
23
|
|
|
private array $messageCallback = []; |
24
|
|
|
private string $tag = 'div'; |
25
|
|
|
private array $tagAttributes = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Specify a form, its attribute. |
29
|
|
|
* |
30
|
|
|
* @param FormModelInterface $formModel Form instance. |
31
|
|
|
* @param string $attribute Form model's property name this widget is rendered for. |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
* |
35
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
36
|
|
|
*/ |
37
|
155 |
|
public function config(FormModelInterface $formModel, string $attribute): self |
38
|
|
|
{ |
39
|
155 |
|
$new = clone $this; |
40
|
155 |
|
$new->formModel = $formModel; |
41
|
155 |
|
$new->attribute = $attribute; |
42
|
155 |
|
return $new; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Whether content should be HTML-encoded. |
47
|
|
|
* |
48
|
|
|
* @param bool $value |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
151 |
|
public function encode(bool $value): self |
53
|
|
|
{ |
54
|
151 |
|
$new = clone $this; |
55
|
151 |
|
$new->encode = $value; |
56
|
151 |
|
return $new; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Error message to display. |
61
|
|
|
* |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
151 |
|
public function message(string $value): self |
65
|
|
|
{ |
66
|
151 |
|
$new = clone $this; |
67
|
151 |
|
$new->message = $value; |
68
|
151 |
|
return $new; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Callback that will be called to obtain an error message. |
73
|
|
|
* |
74
|
|
|
* The signature of the callback must be: |
75
|
|
|
* |
76
|
|
|
* ```php |
77
|
|
|
* [$FormModel, function()] |
78
|
|
|
* ``` |
79
|
|
|
* |
80
|
|
|
* @param array $value |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
152 |
|
public function messageCallback(array $value): self |
85
|
|
|
{ |
86
|
152 |
|
$new = clone $this; |
87
|
152 |
|
$new->messageCallback = $value; |
88
|
152 |
|
return $new; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The tag name of the container element. |
93
|
|
|
* |
94
|
|
|
* Empty to render error messages without container {@see Html::tag()}. |
95
|
|
|
* |
96
|
|
|
* @param string $value |
97
|
|
|
* |
98
|
|
|
* @return static |
99
|
|
|
*/ |
100
|
3 |
|
public function tag(string $value): self |
101
|
|
|
{ |
102
|
3 |
|
$new = clone $this; |
103
|
3 |
|
$new->tag = $value; |
104
|
3 |
|
return $new; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* HTML attributes for the widget container tag. |
109
|
|
|
* |
110
|
|
|
* @param array $value |
111
|
|
|
* |
112
|
|
|
* @return static |
113
|
|
|
* |
114
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
115
|
|
|
*/ |
116
|
151 |
|
public function tagAttributes(array $value): self |
117
|
|
|
{ |
118
|
151 |
|
$new = clone $this; |
119
|
151 |
|
$new->tagAttributes = $value; |
120
|
151 |
|
return $new; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Generates a tag that contains the first validation error of the specified form attribute. |
125
|
|
|
* |
126
|
|
|
* @return string the generated label tag |
127
|
|
|
*/ |
128
|
155 |
|
protected function run(): string |
129
|
|
|
{ |
130
|
155 |
|
$new = clone $this; |
131
|
155 |
|
$error = HtmlFormErrors::getFirstError($new->formModel, $new->attribute); |
132
|
|
|
|
133
|
155 |
|
if ($error !== '' && $new->message !== '') { |
134
|
2 |
|
$error = $new->message; |
135
|
|
|
} |
136
|
|
|
|
137
|
155 |
|
if ($error !== '' && $new->messageCallback !== []) { |
138
|
|
|
/** @var string */ |
139
|
4 |
|
$error = call_user_func($new->messageCallback, $new->formModel, $new->attribute); |
140
|
|
|
} |
141
|
|
|
|
142
|
155 |
|
$html = $new->tag !== '' |
143
|
155 |
|
? CustomTag::name($new->tag) |
144
|
155 |
|
->attributes($new->tagAttributes) |
145
|
155 |
|
->content($error) |
146
|
155 |
|
->encode($new->encode) |
147
|
155 |
|
->render() |
148
|
1 |
|
: $error; |
149
|
|
|
|
150
|
155 |
|
return $error !== '' ? $html : ''; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|