1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Helper; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* HtmlFormErrors renders a list of errors for the specified model attribute. |
11
|
|
|
*/ |
12
|
|
|
final class HtmlFormErrors |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns the errors for all attributes. |
16
|
|
|
* |
17
|
|
|
* @param FormModelInterface $formModel the form object. |
18
|
|
|
* |
19
|
|
|
* @return array the error messages. |
20
|
|
|
*/ |
21
|
1 |
|
public static function getAllErrors(FormModelInterface $formModel): array |
22
|
|
|
{ |
23
|
1 |
|
return $formModel->getFormErrors()->getAllErrors(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the errors for single attribute. |
28
|
|
|
* |
29
|
|
|
* @param FormModelInterface $formModel the form object. |
30
|
|
|
* @param string $attribute attribute name. Use null to retrieve errors for all attributes. |
31
|
|
|
*/ |
32
|
1 |
|
public static function getErrors(FormModelInterface $formModel, string $attribute): array |
33
|
|
|
{ |
34
|
1 |
|
return $formModel->getFormErrors()->getErrors($attribute); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the firsts errors for all attributes as a one-dimensional array. |
39
|
|
|
* |
40
|
|
|
* @param FormModelInterface $formModel the form object. |
41
|
|
|
* |
42
|
|
|
* @return array errors for all attributes as a one-dimensional array. Empty array is returned if no error. |
43
|
|
|
*/ |
44
|
5 |
|
public static function getErrorSummaryFirstErrors(FormModelInterface $formModel): array |
45
|
|
|
{ |
46
|
5 |
|
return $formModel->getFormErrors()->getErrorSummaryFirstErrors(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the errors for all attributes as a one-dimensional array. |
51
|
|
|
* |
52
|
|
|
* @param FormModelInterface $formModel the form object. |
53
|
|
|
* |
54
|
|
|
* @return array errors for all attributes as a one-dimensional array. Empty array is returned if no error. |
55
|
|
|
*/ |
56
|
3 |
|
public static function getErrorSummary(FormModelInterface $formModel): array |
57
|
|
|
{ |
58
|
3 |
|
return $formModel->getFormErrors()->getErrorSummary(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return the attribute first error message. |
63
|
|
|
* |
64
|
|
|
* @param FormModelInterface $formModel the form object. |
65
|
|
|
* @param string $attribute the attribute name or expression. |
66
|
|
|
* |
67
|
|
|
* @return string the error message. Empty string returned if there is no error. |
68
|
|
|
*/ |
69
|
356 |
|
public static function getFirstError(FormModelInterface $formModel, string $attribute): string |
70
|
|
|
{ |
71
|
356 |
|
return $formModel->getFormErrors()->getFirstError(HtmlForm::getAttributeName($formModel, $attribute)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the first error of every attribute in the model. |
76
|
|
|
* |
77
|
|
|
* @param FormModelInterface $formModel the form object. |
78
|
|
|
* |
79
|
|
|
* @return array The first error message for each attribute in a model. Empty array is returned if there is no |
80
|
|
|
* error. |
81
|
|
|
*/ |
82
|
1 |
|
public static function getFirstErrors(FormModelInterface $formModel): array |
83
|
|
|
{ |
84
|
1 |
|
return $formModel->getFormErrors()->getFirstErrors(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a value indicating whether there is any validation error. |
89
|
|
|
* |
90
|
|
|
* @param FormModelInterface $formModel the form object. |
91
|
|
|
* @param string|null $attribute attribute name. Use null to check all attributes. |
92
|
|
|
* |
93
|
|
|
* @return bool whether there is any error. |
94
|
|
|
*/ |
95
|
8 |
|
public static function hasErrors(FormModelInterface $formModel, ?string $attribute = null): bool |
96
|
|
|
{ |
97
|
8 |
|
return $formModel->getFormErrors()->hasErrors($attribute); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|