@@ -53,30 +53,38 @@ public function build():array
5353 $ this ->rules ['trim ' ] = new ValidationRule ($ this ->typeScope ['trim ' ], 'trim ' );
5454 }
5555
56+ foreach ($ this ->model ->attributes as $ attribute ) {
57+ if ($ this ->isIdColumn ($ attribute )) {
58+ continue ;
59+ }
60+ $ this ->defaultRule ($ attribute );
61+ }
62+
5663 if (!empty ($ this ->typeScope ['required ' ])) {
5764 $ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
5865 }
59- if (!empty ($ this ->typeScope ['ref ' ])) {
60- $ this ->addExistRules ($ this ->typeScope ['ref ' ]);
66+
67+ foreach ($ this ->model ->attributes as $ attribute ) {
68+ if ($ this ->isIdColumn ($ attribute )) {
69+ continue ;
70+ }
71+ $ this ->resolveAttributeRules ($ attribute );
6172 }
73+
6274 foreach ($ this ->model ->indexes as $ index ) {
6375 if ($ index ->isUnique ) {
6476 $ this ->addUniqueRule ($ index ->columns );
6577 }
6678 }
67- foreach ($ this ->model ->attributes as $ attribute ) {
68- // column/field/property with name `id` is considered as Primary Key by this library, and it is automatically handled by DB/Yii; so remove it from validation `rules()`
69- if (in_array ($ attribute ->columnName , ['id ' , $ this ->model ->pkName ]) ||
70- in_array ($ attribute ->propertyName , ['id ' , $ this ->model ->pkName ])
71- ) {
72- continue ;
73- }
74- $ this ->resolveAttributeRules ($ attribute );
79+
80+ if (!empty ($ this ->typeScope ['ref ' ])) {
81+ $ this ->addExistRules ($ this ->typeScope ['ref ' ]);
7582 }
7683
7784 if (!empty ($ this ->typeScope ['safe ' ])) {
7885 $ this ->rules ['safe ' ] = new ValidationRule ($ this ->typeScope ['safe ' ], 'safe ' );
7986 }
87+
8088 return $ this ->rules ;
8189 }
8290
@@ -93,7 +101,6 @@ private function resolveAttributeRules(Attribute $attribute):void
93101 }
94102 if ($ attribute ->phpType === 'bool ' || $ attribute ->phpType === 'boolean ' ) {
95103 $ this ->rules [$ attribute ->columnName . '_boolean ' ] = new ValidationRule ([$ attribute ->columnName ], 'boolean ' );
96- $ this ->defaultRule ($ attribute );
97104 return ;
98105 }
99106
@@ -111,13 +118,11 @@ private function resolveAttributeRules(Attribute $attribute):void
111118 }
112119
113120 $ this ->rules [$ key ] = new ValidationRule ([$ attribute ->columnName ], $ attribute ->dbType , $ params );
114- $ this ->defaultRule ($ attribute );
115121 return ;
116122 }
117123
118124 if (in_array ($ attribute ->phpType , ['int ' , 'integer ' , 'double ' , 'float ' ]) && !$ attribute ->isReference ()) {
119125 $ this ->addNumericRule ($ attribute );
120- $ this ->defaultRule ($ attribute );
121126 return ;
122127 }
123128 if ($ attribute ->phpType === 'string ' && !$ attribute ->isReference ()) {
@@ -127,10 +132,8 @@ private function resolveAttributeRules(Attribute $attribute):void
127132 $ key = $ attribute ->columnName . '_in ' ;
128133 $ this ->rules [$ key ] =
129134 new ValidationRule ([$ attribute ->columnName ], 'in ' , ['range ' => $ attribute ->enumValues ]);
130- $ this ->defaultRule ($ attribute );
131135 return ;
132136 }
133- $ this ->defaultRule ($ attribute );
134137 $ this ->addRulesByAttributeName ($ attribute );
135138 }
136139
@@ -278,4 +281,15 @@ public function __toString()
278281 }
279282 };
280283 }
284+
285+ private function isIdColumn (Attribute $ attribute ): bool
286+ {
287+ // column/field/property with name `id` is considered as Primary Key by this library, and it is automatically handled by DB/Yii; so remove it from validation `rules()`
288+ if (in_array ($ attribute ->columnName , ['id ' , $ this ->model ->pkName ]) ||
289+ in_array ($ attribute ->propertyName , ['id ' , $ this ->model ->pkName ])
290+ ) {
291+ return true ;
292+ }
293+ return false ;
294+ }
281295}
0 commit comments