@@ -53,30 +53,38 @@ public function build():array
5353 $ this ->rules ['trim ' ] = new ValidationRule ($ this ->typeScope ['trim ' ], 'trim ' );
5454 }
5555
56- if (!empty ($ this ->typeScope ['ref ' ])) {
57- $ this ->addExistRules ($ this ->typeScope ['ref ' ]);
58- }
59- foreach ($ this ->model ->indexes as $ index ) {
60- if ($ index ->isUnique ) {
61- $ this ->addUniqueRule ($ index ->columns );
56+ foreach ($ this ->model ->attributes as $ attribute ) {
57+ if ($ this ->isIdColumn ($ attribute )) {
58+ continue ;
6259 }
60+ $ this ->defaultRule ($ attribute );
61+ }
62+
63+ if (!empty ($ this ->typeScope ['required ' ])) {
64+ $ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
6365 }
66+
6467 foreach ($ this ->model ->attributes as $ attribute ) {
65- // 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()`
66- if (in_array ($ attribute ->columnName , ['id ' , $ this ->model ->pkName ]) ||
67- in_array ($ attribute ->propertyName , ['id ' , $ this ->model ->pkName ])
68- ) {
68+ if ($ this ->isIdColumn ($ attribute )) {
6969 continue ;
7070 }
7171 $ this ->resolveAttributeRules ($ attribute );
7272 }
7373
74+ foreach ($ this ->model ->indexes as $ index ) {
75+ if ($ index ->isUnique ) {
76+ $ this ->addUniqueRule ($ index ->columns );
77+ }
78+ }
79+
80+ if (!empty ($ this ->typeScope ['ref ' ])) {
81+ $ this ->addExistRules ($ this ->typeScope ['ref ' ]);
82+ }
83+
7484 if (!empty ($ this ->typeScope ['safe ' ])) {
7585 $ this ->rules ['safe ' ] = new ValidationRule ($ this ->typeScope ['safe ' ], 'safe ' );
7686 }
77- if (!empty ($ this ->typeScope ['required ' ])) {
78- $ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
79- }
87+
8088 return $ this ->rules ;
8189 }
8290
@@ -93,7 +101,7 @@ 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 );
104+ // $this->defaultRule($attribute);
97105 return ;
98106 }
99107
@@ -111,13 +119,13 @@ private function resolveAttributeRules(Attribute $attribute):void
111119 }
112120
113121 $ this ->rules [$ key ] = new ValidationRule ([$ attribute ->columnName ], $ attribute ->dbType , $ params );
114- $ this ->defaultRule ($ attribute );
122+ // $this->defaultRule($attribute);
115123 return ;
116124 }
117125
118126 if (in_array ($ attribute ->phpType , ['int ' , 'integer ' , 'double ' , 'float ' ]) && !$ attribute ->isReference ()) {
119127 $ this ->addNumericRule ($ attribute );
120- $ this ->defaultRule ($ attribute );
128+ // $this->defaultRule($attribute);
121129 return ;
122130 }
123131 if ($ attribute ->phpType === 'string ' && !$ attribute ->isReference ()) {
@@ -127,10 +135,10 @@ private function resolveAttributeRules(Attribute $attribute):void
127135 $ key = $ attribute ->columnName . '_in ' ;
128136 $ this ->rules [$ key ] =
129137 new ValidationRule ([$ attribute ->columnName ], 'in ' , ['range ' => $ attribute ->enumValues ]);
130- $ this ->defaultRule ($ attribute );
138+ // $this->defaultRule($attribute); // TODO remove
131139 return ;
132140 }
133- $ this ->defaultRule ($ attribute );
141+ // $this->defaultRule($attribute);
134142 $ this ->addRulesByAttributeName ($ attribute );
135143 }
136144
@@ -278,4 +286,15 @@ public function __toString()
278286 }
279287 };
280288 }
289+
290+ private function isIdColumn (Attribute $ attribute ): bool
291+ {
292+ // 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()`
293+ if (in_array ($ attribute ->columnName , ['id ' , $ this ->model ->pkName ]) ||
294+ in_array ($ attribute ->propertyName , ['id ' , $ this ->model ->pkName ])
295+ ) {
296+ return true ;
297+ }
298+ return false ;
299+ }
281300}
0 commit comments