generated from yiisoft/package-template
/
ErrorSummary.php
179 lines (155 loc) · 4.58 KB
/
ErrorSummary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Yiisoft\Form\Field;
use InvalidArgumentException;
use Yiisoft\Form\Field\Base\BaseField;
use Yiisoft\Form\FormModelInterface;
use Yiisoft\Form\Helper\HtmlFormErrors;
use Yiisoft\Html\Html;
/**
* Displays a summary of the form validation errors. If there is no validation error, field will be hidden.
*/
final class ErrorSummary extends BaseField
{
private ?FormModelInterface $formModel = null;
private bool $encode = true;
private bool $showAllErrors = false;
private array $onlyAttributes = [];
private string $footer = '';
private array $footerAttributes = [];
private string $header = 'Please fix the following errors:';
private array $headerAttributes = [];
public function formModel(FormModelInterface $formModel): self
{
$new = clone $this;
$new->formModel = $formModel;
return $new;
}
/**
* Whether error content should be HTML-encoded.
*/
public function encode(bool $value): self
{
$new = clone $this;
$new->encode = $value;
return $new;
}
/**
* Whether to show all errors.
*/
public function showAllErrors(bool $value = true): self
{
$new = clone $this;
$new->showAllErrors = $value;
return $new;
}
/**
* Specific attributes to be filtered out when rendering the error summary.
*
* @param array $names The attribute names to be included in error summary.
*/
public function onlyAttributes(string ...$names): self
{
$new = clone $this;
$new->onlyAttributes = $names;
return $new;
}
/**
* Set the footer text for the error summary
*
* @param string $value
*
* @return self
*/
public function footer(string $value): self
{
$new = clone $this;
$new->footer = $value;
return $new;
}
/**
* Set footer attributes for the error summary.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return self
*
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function footerAttributes(array $values): self
{
$new = clone $this;
$new->footerAttributes = $values;
return $new;
}
/**
* Set the header text for the error summary
*
* @param string $value
*
* @return self
*/
public function header(string $value): self
{
$new = clone $this;
$new->header = $value;
return $new;
}
/**
* Set header attributes for the error summary.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return self
*
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function headerAttributes(array $values): self
{
$new = clone $this;
$new->headerAttributes = $values;
return $new;
}
protected function generateContent(): ?string
{
$messages = $this->collectErrors();
if (empty($messages)) {
return null;
}
$content = [];
if ($this->header !== '') {
$content[] = Html::p($this->header, $this->headerAttributes)->render();
}
$content[] = Html::ul()
->strings($messages, [], $this->encode)
->render();
if ($this->footer !== '') {
$content[] = Html::p($this->footer, $this->footerAttributes)->render();
}
return implode("\n", $content);
}
/**
* Return array of the validation errors.
*
* @return string[] Array of the validation errors.
*/
private function collectErrors(): array
{
if ($this->formModel === null) {
throw new InvalidArgumentException('Form model is not set.');
}
$errors = HtmlFormErrors::getErrorSummaryFirstErrors($this->formModel);
if ($this->showAllErrors) {
$errors = HtmlFormErrors::getErrorSummary($this->formModel, $this->onlyAttributes);
} elseif ($this->onlyAttributes !== []) {
$errors = array_intersect_key($errors, array_flip($this->onlyAttributes));
}
/**
* If there are the same error messages for different attributes, array_unique will leave gaps between
* sequential keys. Applying array_values to reorder array keys.
*
* @var string[]
*/
return array_values(array_unique($errors));
}
}