Passed
Pull Request — master (#147)
by Wilmer
03:09 queued 51s
created

HtmlFormErrors::getFirstError()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
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 single attribute.
16
     *
17
     * @param FormModelInterface $formModel the form object.
18
     * @param string $attribute attribute name. Use null to retrieve errors for all attributes.
19
     */
20 1
    public static function getError(FormModelInterface $formModel, string $attribute): array
21
    {
22 1
        return $formModel->getFormErrors()->getError($attribute);
23
    }
24
25
    /**
26
     * Returns the errors for all attributes.
27
     *
28
     * @param FormModelInterface $formModel the form object.
29
     *
30
     * @return array the error messages.
31
     */
32
    public static function getErrors(FormModelInterface $formModel): array
33
    {
34
        return $formModel->getFormErrors()->getErrors();
35
    }
36
37
    /**
38
     * Returns the errors for all attributes as a one-dimensional array.
39
     *
40
     * @param FormModelInterface $formModel the form object.
41
     * @param bool $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
42
     * only the first error message for each attribute will be shown.
43
     *
44
     * @return array errors for all attributes as a one-dimensional array. Empty array is returned if no error.
45
     */
46 4
    public static function getErrorSummary(FormModelInterface $formModel, bool $showAllErrors): array
47
    {
48 4
        return $formModel->getFormErrors()->getErrorSummary($showAllErrors);
49
    }
50
51
    /**
52
     * Return the attribute first error message.
53
     *
54
     * @param FormModelInterface $formModel the form object.
55
     * @param string $attribute the attribute name or expression.
56
     *
57
     * @return string the error message. Empty string returned if there is no error.
58
     */
59 149
    public static function getFirstError(FormModelInterface $formModel, string $attribute): string
60
    {
61 149
        return $formModel->getFormErrors()->getFirstError(HtmlForm::getAttributeName($formModel, $attribute));
62
    }
63
64
    /**
65
     * Returns a value indicating whether there is any validation error.
66
     *
67
     * @param FormModelInterface $formModel the form object.
68
     * @param string|null $attribute attribute name. Use null to check all attributes.
69
     *
70
     * @return bool whether there is any error.
71
     */
72 10
    public static function hasErrors(FormModelInterface $formModel, ?string $attribute = null): bool
73
    {
74 10
        return $formModel->getFormErrors()->hasErrors($attribute);
75
    }
76
}
77