Test Failed
Pull Request — master (#160)
by Wilmer
03:21 queued 47s
created

HtmlFormErrors::getFirstErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    public static function getAllErrors(FormModelInterface $formModel): array
22
    {
23
        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 4
    public static function getErrorSummaryFirstErrors(FormModelInterface $formModel): array
45
    {
46 4
        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 160
    public static function getFirstError(FormModelInterface $formModel, string $attribute): string
70
    {
71 160
        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 10
    public static function getFirstErrors(FormModelInterface $formModel): array
83
    {
84 10
        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
    public static function hasErrors(FormModelInterface $formModel, ?string $attribute = null): bool
96
    {
97
        return $formModel->getFormErrors()->hasErrors($attribute);
98
    }
99
}
100