@@ -53,30 +53,38 @@ public function build():array
53
53
$ this ->rules ['trim ' ] = new ValidationRule ($ this ->typeScope ['trim ' ], 'trim ' );
54
54
}
55
55
56
+ foreach ($ this ->model ->attributes as $ attribute ) {
57
+ if ($ this ->isIdColumn ($ attribute )) {
58
+ continue ;
59
+ }
60
+ $ this ->defaultRule ($ attribute );
61
+ }
62
+
56
63
if (!empty ($ this ->typeScope ['required ' ])) {
57
64
$ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
58
65
}
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 );
61
72
}
73
+
62
74
foreach ($ this ->model ->indexes as $ index ) {
63
75
if ($ index ->isUnique ) {
64
76
$ this ->addUniqueRule ($ index ->columns );
65
77
}
66
78
}
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 ' ]);
75
82
}
76
83
77
84
if (!empty ($ this ->typeScope ['safe ' ])) {
78
85
$ this ->rules ['safe ' ] = new ValidationRule ($ this ->typeScope ['safe ' ], 'safe ' );
79
86
}
87
+
80
88
return $ this ->rules ;
81
89
}
82
90
@@ -93,7 +101,6 @@ private function resolveAttributeRules(Attribute $attribute):void
93
101
}
94
102
if ($ attribute ->phpType === 'bool ' || $ attribute ->phpType === 'boolean ' ) {
95
103
$ this ->rules [$ attribute ->columnName . '_boolean ' ] = new ValidationRule ([$ attribute ->columnName ], 'boolean ' );
96
- $ this ->defaultRule ($ attribute );
97
104
return ;
98
105
}
99
106
@@ -111,13 +118,11 @@ private function resolveAttributeRules(Attribute $attribute):void
111
118
}
112
119
113
120
$ this ->rules [$ key ] = new ValidationRule ([$ attribute ->columnName ], $ attribute ->dbType , $ params );
114
- $ this ->defaultRule ($ attribute );
115
121
return ;
116
122
}
117
123
118
124
if (in_array ($ attribute ->phpType , ['int ' , 'integer ' , 'double ' , 'float ' ]) && !$ attribute ->isReference ()) {
119
125
$ this ->addNumericRule ($ attribute );
120
- $ this ->defaultRule ($ attribute );
121
126
return ;
122
127
}
123
128
if ($ attribute ->phpType === 'string ' && !$ attribute ->isReference ()) {
@@ -127,10 +132,8 @@ private function resolveAttributeRules(Attribute $attribute):void
127
132
$ key = $ attribute ->columnName . '_in ' ;
128
133
$ this ->rules [$ key ] =
129
134
new ValidationRule ([$ attribute ->columnName ], 'in ' , ['range ' => $ attribute ->enumValues ]);
130
- $ this ->defaultRule ($ attribute );
131
135
return ;
132
136
}
133
- $ this ->defaultRule ($ attribute );
134
137
$ this ->addRulesByAttributeName ($ attribute );
135
138
}
136
139
@@ -278,4 +281,15 @@ public function __toString()
278
281
}
279
282
};
280
283
}
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
+ }
281
295
}
0 commit comments