@@ -44,6 +44,11 @@ public function labels()
4444 return [];
4545 }
4646
47+ public function getLabel ($ attribute )
48+ {
49+ return $ this ->labels ()[$ attribute ] ?? $ attribute ;
50+ }
51+
4752 public function rules ()
4853 {
4954 return [];
@@ -59,19 +64,19 @@ public function validate()
5964 $ ruleName = $ rule [0 ];
6065 }
6166 if ($ ruleName === self ::RULE_REQUIRED && !$ value ) {
62- $ this ->addError ($ attribute , self ::RULE_REQUIRED );
67+ $ this ->addErrorByRule ($ attribute , self ::RULE_REQUIRED );
6368 }
6469 if ($ ruleName === self ::RULE_EMAIL && !filter_var ($ value , FILTER_VALIDATE_EMAIL )) {
65- $ this ->addError ($ attribute , self ::RULE_EMAIL );
70+ $ this ->addErrorByRule ($ attribute , self ::RULE_EMAIL );
6671 }
6772 if ($ ruleName === self ::RULE_MIN && strlen ($ value ) < $ rule ['min ' ]) {
68- $ this ->addError ($ attribute , self ::RULE_MIN , ['min ' => $ rule ['min ' ]]);
73+ $ this ->addErrorByRule ($ attribute , self ::RULE_MIN , ['min ' => $ rule ['min ' ]]);
6974 }
7075 if ($ ruleName === self ::RULE_MAX && strlen ($ value ) > $ rule ['max ' ]) {
71- $ this ->addError ($ attribute , self ::RULE_MAX );
76+ $ this ->addErrorByRule ($ attribute , self ::RULE_MAX );
7277 }
7378 if ($ ruleName === self ::RULE_MATCH && $ value !== $ this ->{$ rule ['match ' ]}) {
74- $ this ->addError ($ attribute , self ::RULE_MATCH , ['match ' => $ rule ['match ' ]]);
79+ $ this ->addErrorByRule ($ attribute , self ::RULE_MATCH , ['match ' => $ rule ['match ' ]]);
7580 }
7681 if ($ ruleName === self ::RULE_UNIQUE ) {
7782 $ className = $ rule ['class ' ];
@@ -83,7 +88,7 @@ public function validate()
8388 $ statement ->execute ();
8489 $ record = $ statement ->fetchObject ();
8590 if ($ record ) {
86- $ this ->addError ($ attribute , self ::RULE_UNIQUE );
91+ $ this ->addErrorByRule ($ attribute , self ::RULE_UNIQUE );
8792 }
8893 }
8994 }
@@ -108,7 +113,7 @@ public function errorMessage($rule)
108113 return $ this ->errorMessages ()[$ rule ];
109114 }
110115
111- public function addError (string $ attribute , string $ rule , $ params = [])
116+ protected function addErrorByRule (string $ attribute , string $ rule , $ params = [])
112117 {
113118 $ params ['field ' ] ??= $ attribute ;
114119 $ errorMessage = $ this ->errorMessage ($ rule );
@@ -118,6 +123,11 @@ public function addError(string $attribute, string $rule, $params = [])
118123 $ this ->errors [$ attribute ][] = $ errorMessage ;
119124 }
120125
126+ public function addError (string $ attribute , string $ message )
127+ {
128+ $ this ->errors [$ attribute ][] = $ message ;
129+ }
130+
121131 public function hasError ($ attribute )
122132 {
123133 return $ this ->errors [$ attribute ] ?? false ;
0 commit comments