@@ -53,30 +53,38 @@ public function build():array
53
53
$ this ->rules ['trim ' ] = new ValidationRule ($ this ->typeScope ['trim ' ], 'trim ' );
54
54
}
55
55
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 ;
62
59
}
60
+ $ this ->defaultRule ($ attribute );
61
+ }
62
+
63
+ if (!empty ($ this ->typeScope ['required ' ])) {
64
+ $ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
63
65
}
66
+
64
67
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 )) {
69
69
continue ;
70
70
}
71
71
$ this ->resolveAttributeRules ($ attribute );
72
72
}
73
73
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
+
74
84
if (!empty ($ this ->typeScope ['safe ' ])) {
75
85
$ this ->rules ['safe ' ] = new ValidationRule ($ this ->typeScope ['safe ' ], 'safe ' );
76
86
}
77
- if (!empty ($ this ->typeScope ['required ' ])) {
78
- $ this ->rules ['required ' ] = new ValidationRule ($ this ->typeScope ['required ' ], 'required ' );
79
- }
87
+
80
88
return $ this ->rules ;
81
89
}
82
90
@@ -93,7 +101,7 @@ 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 );
104
+ // $this->defaultRule($attribute);
97
105
return ;
98
106
}
99
107
@@ -111,13 +119,13 @@ private function resolveAttributeRules(Attribute $attribute):void
111
119
}
112
120
113
121
$ this ->rules [$ key ] = new ValidationRule ([$ attribute ->columnName ], $ attribute ->dbType , $ params );
114
- $ this ->defaultRule ($ attribute );
122
+ // $this->defaultRule($attribute);
115
123
return ;
116
124
}
117
125
118
126
if (in_array ($ attribute ->phpType , ['int ' , 'integer ' , 'double ' , 'float ' ]) && !$ attribute ->isReference ()) {
119
127
$ this ->addNumericRule ($ attribute );
120
- $ this ->defaultRule ($ attribute );
128
+ // $this->defaultRule($attribute);
121
129
return ;
122
130
}
123
131
if ($ attribute ->phpType === 'string ' && !$ attribute ->isReference ()) {
@@ -127,10 +135,10 @@ private function resolveAttributeRules(Attribute $attribute):void
127
135
$ key = $ attribute ->columnName . '_in ' ;
128
136
$ this ->rules [$ key ] =
129
137
new ValidationRule ([$ attribute ->columnName ], 'in ' , ['range ' => $ attribute ->enumValues ]);
130
- $ this ->defaultRule ($ attribute );
138
+ // $this->defaultRule($attribute); // TODO remove
131
139
return ;
132
140
}
133
- $ this ->defaultRule ($ attribute );
141
+ // $this->defaultRule($attribute);
134
142
$ this ->addRulesByAttributeName ($ attribute );
135
143
}
136
144
@@ -278,4 +286,15 @@ public function __toString()
278
286
}
279
287
};
280
288
}
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
+ }
281
300
}
0 commit comments